Jump to: navigation, search

Difference between revisions of "EclipseLink/Examples/JPA/OutsideContainer"

(Customizing Persistence Unit using Java)
Line 1: Line 1:
 
Users may also use the EntityManager API outside the container by creating a EntityManagerFactory for a given persistence unit name and properties map (includes database information etc.)
 
Users may also use the EntityManager API outside the container by creating a EntityManagerFactory for a given persistence unit name and properties map (includes database information etc.)
  
<code><pre>
+
<source lang="java">
 
EntityManagerFactory emf = Persistence.createEntityManagerFactory(persistenceUnitName, propertiesMap);
 
EntityManagerFactory emf = Persistence.createEntityManagerFactory(persistenceUnitName, propertiesMap);
 
EntityManager em = emf.createEntityManager();
 
EntityManager em = emf.createEntityManager();
</pre></code>
+
</source>
  
 
== JavaSE Configuration using Property Map ==
 
== JavaSE Configuration using Property Map ==
Line 10: Line 10:
 
When a simple persistence unit configuration is defined which relies on container deployment:
 
When a simple persistence unit configuration is defined which relies on container deployment:
  
<pre>
+
<source lang="xml">
<persistence-unit name="employee" transaction-type="RESOURCE_LOCAL">
+
<persistence-unit name="employee" transaction-type="RESOURCE_LOCAL">
<non-jta-data-source>jdbc/MyDS</non-jta-data-source>
+
    <non-jta-data-source>jdbc/MyDS</non-jta-data-source>
</persistence-unit>
+
</persistence-unit>
</pre>
+
</source>
  
 
In order to test this persistence unit outside of the container the JavaSE bootstrapping API must be used. the following code can be used to customize the configuration:
 
In order to test this persistence unit outside of the container the JavaSE bootstrapping API must be used. the following code can be used to customize the configuration:
  
<pre>
+
<source>
import static org.eclipse.persistence.jpa.config.PersistenceUnitProperties.*;
+
import static org.eclipse.persistence.config.PersistenceUnitProperties.*;
  
...
+
    ...
  
Map properties = new HashMap();
+
    Map properties = new HashMap();
  
// Ensure RESOURCE_LOCAL transactions is used.
+
    // Ensure RESOURCE_LOCAL transactions is used.
properties.put(TRANSACTION_TYPE,
+
    properties.put(TRANSACTION_TYPE,
PersistenceUnitTransactionType.RESOURCE_LOCAL.name());
+
        PersistenceUnitTransactionType.RESOURCE_LOCAL.name());
  
// Configure the internal EclipseLink connection pool
+
    // Configure the internal EclipseLink connection pool
properties.put(JDBC_DRIVER, "oracle.jdbc.OracleDriver");
+
    properties.put(JDBC_DRIVER, "oracle.jdbc.OracleDriver");
properties.put(JDBC_URL, "jdbc:oracle:thin:@localhost:1521:ORCL");
+
    properties.put(JDBC_URL, "jdbc:oracle:thin:@localhost:1521:ORCL");
properties.put(JDBC_USER, "scott");
+
    properties.put(JDBC_USER, "scott");
properties.put(JDBC_PASSWORD, "tiger");
+
    properties.put(JDBC_PASSWORD, "tiger");
properties.put(JDBC_READ_CONNECTIONS_MIN, "1");
+
    properties.put(JDBC_READ_CONNECTIONS_MIN, "1");
properties.put(JDBC_WRITE_CONNECTIONS_MIN, "1");
+
    properties.put(JDBC_WRITE_CONNECTIONS_MIN, "1");
  
// Configure logging. FINE ensures all SQL is shown
+
    // Configure logging. FINE ensures all SQL is shown
properties.put(LOGGING_LEVEL, "FINE");
+
    properties.put(LOGGING_LEVEL, "FINE");
properties.put(LOGGING_TIMESTAMP, "false");
+
    properties.put(LOGGING_TIMESTAMP, "false");
properties.put(LOGGING_THREAD, "false");
+
    properties.put(LOGGING_THREAD, "false");
properties.put(LOGGING_SESSION, "false");
+
    properties.put(LOGGING_SESSION, "false");
  
// Ensure that no server-platform is configured
+
    // Ensure that no server-platform is configured
properties.put(TARGET_SERVER, TargetServer.None);
+
    properties.put(TARGET_SERVER, TargetServer.None);
</pre>
+
</source>
  
 
Now the PU can be instantiated for testing using:
 
Now the PU can be instantiated for testing using:
  
<pre>
+
<source lang="java">
Persistence.createEntityManagerFactory(unitName, properties);
+
Persistence.createEntityManagerFactory(unitName, properties);
</pre>
+
</source>

Revision as of 09:29, 19 June 2008

Users may also use the EntityManager API outside the container by creating a EntityManagerFactory for a given persistence unit name and properties map (includes database information etc.)

EntityManagerFactory emf = Persistence.createEntityManagerFactory(persistenceUnitName, propertiesMap);
EntityManager em = emf.createEntityManager();

JavaSE Configuration using Property Map

When a simple persistence unit configuration is defined which relies on container deployment:

<persistence-unit name="employee" transaction-type="RESOURCE_LOCAL">
    <non-jta-data-source>jdbc/MyDS</non-jta-data-source>
</persistence-unit>

In order to test this persistence unit outside of the container the JavaSE bootstrapping API must be used. the following code can be used to customize the configuration:

import static org.eclipse.persistence.config.PersistenceUnitProperties.*;

    ...

    Map properties = new HashMap();

    // Ensure RESOURCE_LOCAL transactions is used.
    properties.put(TRANSACTION_TYPE,
        PersistenceUnitTransactionType.RESOURCE_LOCAL.name());

    // Configure the internal EclipseLink connection pool
    properties.put(JDBC_DRIVER, "oracle.jdbc.OracleDriver");
    properties.put(JDBC_URL, "jdbc:oracle:thin:@localhost:1521:ORCL");
    properties.put(JDBC_USER, "scott");
    properties.put(JDBC_PASSWORD, "tiger");
    properties.put(JDBC_READ_CONNECTIONS_MIN, "1");
    properties.put(JDBC_WRITE_CONNECTIONS_MIN, "1");

    // Configure logging. FINE ensures all SQL is shown
    properties.put(LOGGING_LEVEL, "FINE");
    properties.put(LOGGING_TIMESTAMP, "false");
    properties.put(LOGGING_THREAD, "false");
    properties.put(LOGGING_SESSION, "false");

    // Ensure that no server-platform is configured
    properties.put(TARGET_SERVER, TargetServer.None);

Now the PU can be instantiated for testing using:

Persistence.createEntityManagerFactory(unitName, properties);