我正在使用JBoss Wildfly作为我的JPA层上的应用程序服务器 .
对于技术要求,我需要使用JavaSE /应用程序管理方法获取我的实体持久性管理器 . 我:
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();
在我设置的属性中:
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);
当然,问题在于上面的代码行我不能将entitymanager绑定到容器JTA事务管理器 .
所以我的问题是:是否有一些示例或某种方式可以让实体管理器加入复杂的JTA事务?我不知道......也许我可以将CDI 生产环境 者放入容器上下文中的实体管理器?
1 回答
在Java EE环境中,您可以注入EntityManagerFactory并使用它来创建具有自定义属性的EntityManager . 因此,而不是
你应该做的事情如下: