Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

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

(New page: Users may also use the EntityManager API outside the container. You may use the org.eclipse.persistence.jpa.JpaHelper class to obtain an EntityManager. == Using a server session == <code>...)
 
m
 
(14 intermediate revisions by 3 users not shown)
Line 1: Line 1:
Users may also use the EntityManager API outside the container. You may use the org.eclipse.persistence.jpa.JpaHelper class to obtain an EntityManager.
+
Users may also use the EntityManager API outside the container, either through configuring a <code>persistence.xml</code>, or by creating a EntityManagerFactory for a given persistence unit name and properties map (includes database information etc.)
  
== Using a server session ==
+
<source lang="java">
<code><pre>
+
EntityManagerFactory emf = Persistence.createEntityManagerFactory(persistenceUnitName, propertiesMap);
EntityManagerFactory emf = org.eclipse.persistence.jpa.JpaHelper.createEntityManagerFactory(serverSession);
+
 
EntityManager em = emf.createEntityManager();
 
EntityManager em = emf.createEntityManager();
</pre><code>
+
</source>
  
== Using a sessions.xml file ==
 
<code><pre>
 
EntityManagerFactory emf = org.eclipse.persistence.jpa.JpaHelper.createEntityManagerFactory(sessionName);
 
EntityManager em = emf.createEntityManager();
 
</pre><code>
 
  
Or you may use an EntityManager from a persistence.xml file.
+
== JavaSE Configuration using persistence.xml ==
  
== Using a persistence unit name ==
+
<source lang="xml">
<code><pre>
+
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
EntityManagerFactory emf = Persistence.createEntityManagerFactory(persistenceUnitName, propertiesMap);
+
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
EntityManager em = create.getEntityManager();
+
        xsi:schemaLocation="http://java.sun.com/xml/ns/persistence persistence_1_0.xsd"
</pre></code>
+
        version="1.0">
 +
  <persistence-unit name="my-app" transaction-type="RESOURCE_LOCAL">
 +
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
 +
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
 +
    <properties>
 +
      <property name="javax.persistence.jdbc.driver" value="oracle.jdbc.OracleDriver"/>
 +
      <property name="javax.persistence.jdbc.url" value="jdbc:oracle:thin:@localhost:1521:orcl"/>
 +
      <property name="javax.persistence.jdbc.user" value="scott"/>
 +
      <property name="javax.persistence.jdbc.password" value="tiger"/>
 +
    </properties>
 +
  </persistence-unit>
 +
</persistence>
 +
</source>
 +
 
 +
Note, <code>"javax.persistence.jdbc"</code> is used in JPA 2.0, as of EclipseLink 1.2, previously <code>"eclipselink.jdbc"</code> must be used.
 +
 
 +
Now the EntityManagerFactory can be instantiated using:
 +
 
 +
<source lang="java">
 +
Persistence.createEntityManagerFactory("my-app");
 +
</source>
 +
 
 +
== JavaSE Configuration using Property Map ==
 +
 
 +
When a simple persistence unit configuration is defined which relies on container deployment:
 +
 
 +
<source lang="xml">
 +
<persistence-unit name="employee" transaction-type="RESOURCE_LOCAL">
 +
    <non-jta-data-source>jdbc/MyDS</non-jta-data-source>
 +
</persistence-unit>
 +
</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:
 +
 
 +
<source lang="java">
 +
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");
 +
 
 +
    // 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);
 +
</source>
 +
 
 +
Now the EntityManagerFactory can be instantiated for testing using:
 +
 
 +
<source lang="java">
 +
Persistence.createEntityManagerFactory(unitName, properties);
 +
</source>
 +
 
 +
 
 +
----
 +
 
 +
'''For more information, see "Testing JPA Outside a Container" in the ''[http://www.eclipse.org/eclipselink/documentation/ EclipseLink Solutions Guide]''.'''
  
Users will then need to specify the database properties in the propertiesMap and are resonsible for building their persistence.xml file and having it available on the classpath.
+
[[Category:EclipseLink/Example/JPA|OutsideContainer]]

Latest revision as of 19:05, 1 February 2013

Users may also use the EntityManager API outside the container, either through configuring a persistence.xml, or 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 persistence.xml

<persistence xmlns="http://java.sun.com/xml/ns/persistence"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/persistence persistence_1_0.xsd"
        version="1.0">
  <persistence-unit name="my-app" transaction-type="RESOURCE_LOCAL">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <properties>
      <property name="javax.persistence.jdbc.driver" value="oracle.jdbc.OracleDriver"/>
      <property name="javax.persistence.jdbc.url" value="jdbc:oracle:thin:@localhost:1521:orcl"/>
      <property name="javax.persistence.jdbc.user" value="scott"/>
      <property name="javax.persistence.jdbc.password" value="tiger"/>
    </properties>
  </persistence-unit>
</persistence>

Note, "javax.persistence.jdbc" is used in JPA 2.0, as of EclipseLink 1.2, previously "eclipselink.jdbc" must be used.

Now the EntityManagerFactory can be instantiated using:

Persistence.createEntityManagerFactory("my-app");

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");
 
    // 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 EntityManagerFactory can be instantiated for testing using:

Persistence.createEntityManagerFactory(unitName, properties);



For more information, see "Testing JPA Outside a Container" in the EclipseLink Solutions Guide.

Copyright © Eclipse Foundation, Inc. All Rights Reserved.