Project Versions
EclipseLink Version: 2.5.2 - [ EclipseLink 2.5.2 == JPA 2.1]
JBDC Driver Name : Oracle JDBC driver
JDBC Driver Version : 12.1.0.2.0
Database Version : Database Product Version: Oracle Database 12c Enterprise Edition Release 12.1.0.2.0 - 64bit Production
With the Partitioning, Real Application Clusters, Automatic Storage Management, OLAP,
Advanced Analytics and Real Application Testing options
Database Name and Instances : DB Name: BANKA
1. Instance Name: bankA_2 - Host: hract21.example.com
2. Instance Name: bankA_1 - Host: hract22.example.com
Netbeans Version : 8.0.2
Wildfly Version : 8.2
Entity Manger Type : Application Managed
Transaction Type : JTA
Setup EclipseLink as a Wildfly Module
Check modules/system/layers/base/org/eclipse/persistence/main directory
oracle@wls1 main]$ pwd
/usr/local/wildfly-8.2.0.Final/modules/system/layers/base/org/eclipse/persistence/main
Copy eclipselink-2.5.2.jar from MAVEN repository to modules/system/layers/base/org/eclipse/persistence/main
[oracle@wls1 main]$ ls
eclipselink-2.5.2.jar jipijapa-eclipselink-1.0.1.Final.jar module.xml
Add eclipselink-2.5.2.jar to module.xml in modules/system/layers/base/org/eclipse/persistence/main
[oracle@wls1 main]$ cat module.xml
<module xmlns="urn:jboss:module:1.3" name="org.eclipse.persistence">
<resources>
<resource-root path="jipijapa-eclipselink-1.0.1.Final.jar"/>
<resource-root path="eclipselink-2.5.2.jar"/>
</resources>
<dependencies>
<module name="asm.asm"/>
<module name="javax.api"/>
<module name="javax.annotation.api"/>
<module name="javax.enterprise.api"/>
<module name="javax.persistence.api"/>
<module name="javax.transaction.api"/>
<module name="javax.validation.api"/>
<module name="javax.xml.bind.api"/>
<module name="org.antlr"/>
<module name="org.apache.commons.collections"/>
<module name="org.dom4j"/>
<module name="org.javassist"/>
<module name="org.jboss.as.jpa.spi"/>
<module name="org.jboss.logging"/>
<module name="org.jboss.vfs"/>
</dependencies>
Configure your pom.xml
- Remove the hibernate reference and add the EclipseLink reference
As we provided the EclipseLink Jar as a Wildlfly module add : <scope>provided</scope>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>eclipselink</artifactId>
<version>2.5.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>org.eclipse.persistence.jpa.modelgen.processor</artifactId>
<version>2.5.2</version>
<scope>provided</scope>
</dependency>
Create a new Netbeans project
Create new Maven project : Maven -> Web Application
Add JSF support : Properties -> Add Framework -> JSF 2.2
Source Package -> New -> Entity Class from Database
Add New File -> Persistence -> Entity Class from Database
-> Select Datasource:
Host : ract2-scan.grid12c.example.com
Port : 1521/banka
URL : jdbc:oracle:thin:@ract2-scan.grid12c.example.com:1521/banka
-> Select table : EMP2
Select ;
x Generate Named Query Annotations for Persistent Fields
x Generate JAXB Annotations
x Create Persistence Unit
Create persistence.xml
New File -> Persistence -> Create new Persistence Unit
-> Select Datasource: ..
x Use Java Transaction API
A working persistence.xml sample may look like :
<?xml version="1.0" encoding="UTF-8"?>
<!-- <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
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0"> -->
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="jpaPU" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>java:/jboss/datasources/myRacDS</jta-data-source>
<jar-file>MyFirstRACPU.jar</jar-file>
<class>com.hhu.wfjpa2el.Emp2</class>
<class>com.hhu.wfjpa2el.Logemp2</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="eclipselink.logging.level" value="FINEST"/>
<property name="eclipselink.cache.shared.default" value="false"/>
</properties>
</persistence-unit>
</persistence>
- JTA is used and the datasoruce name is : java:/jboss/datasources/myRacDS
- org.eclipse.persistence.jpa.PersistenceProvider is the Persistence Provider
- The perstistence Unit name is jpaPU [ this is used by our JAVA code ]
JAVA Code to create a thread safe access to the Entity Manager
- Persistence.createEntityManagerFactory(“jpaPU“) must match <persistence-unit name=”jpaPU” transaction-type=”JTA“>
- <class>com.hhu.wfjpa2el.Logemp2</class> must match the Classes we have created by using Netbeans menu option : Create Entity Class from Database
- For the Entity Manger access we use the “ThreadLocal Pattern”
static
{
emf = Persistence.createEntityManagerFactory("jpaPU");
threadLocal = new ThreadLocal<EntityManager>();
}
public static EntityManager getEntityManager()
{
EntityManager em = threadLocal.get();
if (em == null)
{
// setRunTimeInfo(getRunTimeInfo() + Tools.add_hmtl_pre_tag("Creating Entity Manager ! " ));
logger.info("Creating Entity Manager Factory ..." );
em = emf.createEntityManager();
// set your flush mode here
threadLocal.set(em);
}
return em;
}
public static void closeEntityManager()
{
EntityManager em = threadLocal.get();
if (em != null)
{
logger.info("Closing Entity Manager" );
em.close();
threadLocal.set(null);
}
}
Note for each request you have to open and close the Entity Manager
..
em=getEntityManager();
..
closeEntityManager();
..
A more Complete Code fragment
public String addEntity()
{
EntityManager em;
String methodName = "addEntity()";
short eno= getEmpno();
cleanall();
try
{
setRunTimeInfo("Calling " + methodName + "in progress - ID: " + eno + " - useJoinTransaction : " + isUseJoinTransaction() );
Emp2 e = new Emp2(eno);
e.setEname("Helmut");
e.setJob("Progr.");
e.setSal(new BigDecimal(1000.0));
setRunTimeInfo("Requesting Entity Manager.. ");
em=getEntityManager();
ut.begin();
em.joinTransaction();
setRunTimeInfo("Running em.persists() ... ");
em.persist(e);
setRunTimeInfo("Running em.flush() ... ");
em.flush();
setRunTimeInfo("Running ut.commit() ... ");
ut.commit();
setRunTimeInfo("Closing Entity Manager.. !");
setRunTimeInfo("Leaving " + methodName + " without Exceptions !");
}
catch ( Throwable t1)
{
jpa_exception(t1, methodName);
}
closeEntityManager();
setRunTimeInfo("Leaving " + methodName + " - Entity manager closed !\n");
return "index";
}
The related Server.log File shoud look like :
2015-05-20 18:38:23,361 INFO [org.eclipse.persistence] (default task-5) EclipseLink, version: Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd
2015-05-20 18:38:23,496 INFO [org.eclipse.persistence.connection] (default task-5) connecting(DatabaseLogin(
2015-05-20 18:38:23,497 INFO [org.eclipse.persistence.connection] (default task-5) Connected: jdbc:oracle:thin:@ract2-scan.grid12c.example.com:1521/banka
2015-05-20 18:38:23,497 INFO [org.eclipse.persistence.connection] (default task-5) connecting(DatabaseLogin(
2015-05-20 18:38:23,497 INFO [org.eclipse.persistence.connection] (default task-5) Connected: jdbc:oracle:thin:@ract2-scan.grid12c.example.com:1521/banka
2015-05-20 18:38:23,653 INFO [org.eclipse.persistence.connection] (default task-5) vfs:/usr/local/wildfly-8.2.0.Final/standalone/deployments/t-1.0.war/WEB-INF/classes/_jpaPU login successful
One thought on “Using Eclipselink as a Persistence Provider in a Netbeans/Wildfly project for JTA transaction”