如何在不使用”persistence.xml”的情况下以编程方式配置transaction-type=”JTA”?

huangapple go评论74阅读模式
英文:

How to configure transaction-type="JTA" programmatically (without persistence.xml")?

问题

我可以在配置的Java类中创建EntityManagerFactory时设置transaction-type为"JTA",而无需任何.xml配置吗?

英文:

Can I set transaction-type "JTA" when creating EntityManagerFactory in configuration java class without any .xml configs?

答案1

得分: 3

你可以尝试以以下方式覆盖javax.persistence.transactionType属性:

Map configOverrides = new HashMap();
configOverrides.put("javax.persistence.transactionType", "JTA");
EntityManagerFactory emf = Persistence.createEntityManagerFactory("yourPersistenceUnitName", configOverrides);

但是如果在类路径中不存在META-INF/persistence.xml文件,可能会引发PersistenceException异常。

请参阅文档2.2.2. 启动部分)。

英文:

You can try to override the javax.persistence.transactionType property in this way:

Map configOverrides = new HashMap();
configOverrides.put("javax.persistence.transactionType", "JTA");
EntityManagerFactory emf = Persistence.createEntityManagerFactory("yourPersistenceUnitName", configOverrides);

But it looks like the PersistenceException exception will be raised if the META-INF/persistence.xml file is absent in your classpath.

See the documentation (section 2.2.2. Bootstrapping).

huangapple
  • 本文由 发表于 2020年9月7日 16:21:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/63773846.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定