Integration Hibernate-JTA-JPA-EJB-GlassFish-MySQL:
1- Hibernate-JPA-EJB-GlassFish-MySql:
This guidance is for integrating hibernate.4.3.5 and EJB and GlassFish.4.0 in NetBeans.8.0 IDE.
Create a web project in net beans and add hibernate jar files to the project,other setting related to configuring MySql and glassfish is very simple so I do not describe in this article then create persistence.xml file as follow :
<persistence-unit name="omidashouriPU" transaction-type="Resource_Local">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="hibernate.archive.autodetection" value="class"/>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/YourSchemaName"/>
<property name="hibernate.connection.username" value="root"/>
<property name="hibernate.connection.password" value="yourpassword"/>
<property name="hibernate.show_sql" value="true"/>
</properties>
</persistence-unit>
In Your EJB class (class that annotated with @Stateless) for creating EntityManager use following syntax :
EntityManagerFactory emf = Persistence.createEntityManagerFactory("omidashouriPU");
EntityManager em = emf.createEntityManager();
em = emf.createEntityManager();
em.getTransaction().begin();
em.persist(YourEntityObject);
em.getTransaction().end();
As you Know when you are using “transaction-type="Resource_Local", you have to manage the transaction by yourself, mean that, managing of opening and closing the transaction is our responsibility.
2- Hibernate-JPA-JTA-EJB-GlassFish-MySql:
This guidance is for integrating hibernate.4.3.5 and EJB and JTA and GlassFish.4.0 in NetBeans.8.0 IDE. Create a web project in net beans (attention: do not make web project with maven because there is a bug in Netbeans.8.0 IDE ) and add hibernate jar files to the project, other setting related to configuring MySql and glassfish is very simple(Just define Connection Pool and JDBC in Resources>JDBC:JDBC Connection Pools & JDBC Resources, guidance for that is in the web if you search for it)(attention: for defining a correct JNDI, first create a temporary project which depends on JNDI like JPA project in glassfish, then copy the settings that is created in Glassfish for this Project because there is a bug in glassfish when you are going to get a ping to MySQl in creating your first Connection Pool if you create it by yourself inside the glassfish) so I do not describe in this article then create persistence.xml file as follow :
<persistence-unit name="omidashouriPU" transaction-type="JTA">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<jta-data-source>jdbc/yourJNDI (which you defined in glassfish) </jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="hibernate.archive.autodetection" value="class"/>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
<property name="hibernate.transaction.jta.platform" value="org.hibernate.service.jta.platform.internal.SunOneJtaPlatform"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/YourSchemaName"/>
<property name="hibernate.connection.username" value="root"/>
<property name="hibernate.connection.password" value="yourpassword"/>
<property name="hibernate.show_sql" value="true"/>
</properties>
</persistence-unit>
In Your EJB class (class that annotated with @Stateless) for creating EntityManager use following syntax :
@PersistenceContext(unitName = " omidashouriPU ")
EntityManager em;
em.persist(YourEntityObject);
As you Know when you are using “transaction-type="JTA", management of transaction is not with you, mean that, managing of opening and closing the transaction is application server (Here GlassFish) responsibility. Indeed if you check your persistence.xml in mode design, in front of persistence provider drop down box you can see hibernate is now added.
Dear Reader, I had Spend 3 days for solving this problem, please add your experience to this article in order that complete it, for any question you can send me email at [email protected] .