0

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?

Roman Melnyk
  • 1,097
  • 1
  • 8
  • 21
Alex
  • 1,515
  • 2
  • 22
  • 44

1 Answers1

0

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);
OndroMih
  • 7,280
  • 1
  • 26
  • 44
  • thanks...but this way i cross over my dynamicity requirements. I need to initialize EntityManagerFactory with the SE createEntityManagerFactory because i have to set datasource at runtime and to change it dynamically. – Alex Oct 08 '15 at 10:36
  • Passing properties into `emf.createEntityManager(properties)` is not dynamic enough? Or the problem is that it ignores properties that modify datasource? – OndroMih Oct 08 '15 at 10:50
  • oh... sorry... i missed that (properties) ... surely i have to try... let you know...!!! thanks – Alex Oct 08 '15 at 11:10
  • well, i'm using Jersy and HK2... @PersistenceUnit( unitName= "KAS-Mapping-AppHandled") doesn't inject anything... do you know how i can lookup for the EntityManagerFactory? – Alex Oct 12 '15 at 13:44
  • Well, in case you are not using Java EE features, you need to use your app server specific configuration. For Wildfly, try [this recipe](https://docs.jboss.org/author/display/WFLY8/JPA+Reference+Guide#JPAReferenceGuide-BindingEntityManagerFactory%2FEntityManagertoJNDI) – OndroMih Oct 12 '15 at 16:04
  • i'm able to get the entity manager factory thanks to your suggestion. But now, when i attach it to the usertransaction (i got it byt ut = (UserTransaction)new InitialContext().lookup("java:comp/UserTransaction");) i get a TABLE NOT FOUND exception after the commit. The trick is the exception is from the h2 driver.... but i set my datasource to work with oracle jdbc – Alex Oct 13 '15 at 10:05
  • This doesn't occur if i specify the datasource inside the persistence.xml, but i cannot do this as it's a mandatory requirements. Datasource have to be attached at runtime. – Alex Oct 13 '15 at 16:01
  • I'm sorry, I'm out of ideas so far – OndroMih Oct 13 '15 at 20:04