0
votes

I'm working with JBoss Wildfly as an application server on my JPA layer.

For technical requirements i need to get my entity persistence manager using the JavaSE/application managed approach. I. e.:

EntityManagerFactory emf = Persistence.createEntityManagerFactory("idelivery", properties); 
EntityManager em = emf.createEntityManager();
MyEntity exUser= new MyEntity();
try{
    Context context = new InitialContext();
    UserTransaction userTransaction = (UserTransaction)context.lookup("java:comp/UserTransaction");
    userTransaction.begin();
    em.persist(exUser);
    userTransaction.commit();

where in properties i set:

properties.put ("hibernate.dialect", "org.hibernate.dialect.Oracle10gDialect");
    properties.put("javax.persistence.provider", "org.hibernate.jpa.HibernatePersistenceProvider"); 
    properties.put("javax.persistence.transactionType", "JTA");
    properties.put("javax.persistence.jtaDataSource", dataSourcePath); 

The problem, of course, is with the code lines above I cannot bind the entitymanager to the container JTA transaction manager.

So my question is: is there some example or some way I can make the entity manager to join a complicated JTA transaction? I don't know... maybe with a CDI producer the way i can put the entitymanager inside the container context?

1

1 Answers

0
votes

In Java EE environment, you may inject EntityManagerFactory and use it to create EntityManager with custom properties. Therefore, instead of

EntityManagerFactory emf = Persistence.createEntityManagerFactory("idelivery", properties); 
EntityManager em = emf.createEntityManager();

you should do something like:

// inject emf from container
@PersistenceUnit("idelivery")
private EntityManagerFactory emf;

// and in your method create em with your properties...
EntityManager em = emf.createEntityManager(properties);