Hibernate version: annotations 3.3.0.GA, entity manager 3.3.1.GA, core 3.2.5.GA
Hi everyone,
I'm developing a web application deployed in GlassFish v2 using the Hibernate components/versions listed above.
My
persistence.xml file looks like the following:
Code:
<persistence version="1.0" 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_1_0.xsd">
<persistence-unit name="JomarAPIServerPU" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>jdbc/JomarDatabase</jta-data-source>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.DB2Dialect"/>
</properties>
</persistence-unit>
</persistence>
I'm having problems finding the right combination of @Resource, @PersistenceUnit and @PersistenceContext.
If I use @PersistenceUnit(name="JomarAPIServerPU") I can load entities correctly, but since I've configured it to use JTA I can't use em.getTransaction(). So I added @Resource UserTransaction and somehow the transaction instance is getting injected because the object is not null when I debug it. However, it appears the Entity Manager is not "bound" to the UserTransaction injected because even when I don't get any exception, the changes are not committed to the database.
The relevant parts of the code in this case are:
Code:
@Resource
private UserTransaction tx;
@PersistenceUnit(unitName = "JomarAPIServerPU")
private EntityManagerFactory emf;
private void executeUpdateLogisticCenters(HttpServletRequest request, HttpServletResponse response, Document requestData) throws Exception {
EntityManager em = emf.createEntityManager();
...
try {
tx.begin();
customer = em.find(customerId);
plant = em.find(plantId);
warehouse = em.find(warehouseId);
logisticCenter = new MrpLogisticCenter();
logisticCenter.setCusid(customer);
logisticCenter.setPltid(plant);
logisticCenter.setWhsid(warehouse);
logisticCenter.setProjectCode(projectCode);
em.persist(logisticCenter);
tx.commit();
} catch (Exception e) {
e.printStackTrace();
if (tx != null) {
tx.rollback();
}
throw new RuntimeException(e.getMessage());
}
}
I also tried using ...
Code:
@PersistenceContext
private EntityManager em;
... and no transaction at all as suggested in Hibernate EntityManager documentation for CMT but I received an exception because a transaction was required and no transaction was available when calling the
persist method.
Finally, I also tried using @Resource for both the UserTransaction and the EntityManagerFactory as suggested in Hibernate EntityManager documentation for BMT, like this:
Code:
@Resource
private UserTransaction tx;
@Resource
private EntityManagerFactory emf;
... but I got an Injection Exception like this one:
Code:
com.sun.enterprise.InjectionException: Exception attempting to inject Env-Prop: tag.jomarAPI.servlet.UpdateServlet/emf@Field-Injectable Resource. Class name = tag.jomarAPI.servlet.UpdateServlet Field name=emf@java.lang.String@tag.jomarAPI.servlet.UpdateServlet/emf@@ into class tag.jomarAPI.servlet.UpdateServlet
Bottom line, I'm obviously mixing different methods of setting up my persistence environment and probably even mixing methods used in different versions of Hibernate or in different combinations of Hibernate components.
If I'm making an obvious mistake or missing a part of the process here, can someone point me out to it? I have been unable to find a complete, working example of how to use EJB3 CMT-JTA persistence in a web application in GlassFish V2 using Hibernate Core, EntityManager and Annotations.
Thank you very much,