JPA 2 具有托管事务而不是持久对象
我正在使用 JPA 2 的 OpenJPA 实现,并且在将对象持久保存到数据库时遇到问题。我想使用由容器(Websphere)管理的事务,所以我的理解是,由于事务是管理的,所以不需要像 tx.begin()、tx.commit() 等样板代码。
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.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_2_0.xsd">
<persistence-unit name="buildTest" transaction-type="JTA">
<jta-data-source>jdbc/lfcbuild</jta-data-source>
<class>entities.Build</class>
<properties>
<property name="openjpa.jdbc.Schema" value="APP"/>
<property name="openjpa.TransactionMode" value="managed"/>
<property name="openjpa.ConnectionFactoryMode" value="managed"/>
</properties>
</persistence-unit>
</persistence>
我在持久化构建时执行的代码是这样的:
EntityManagerFactory emf = Persistence.createEntityManagerFactory("buildTest");
EntityManager em = emf.createEntityManager();
Build b = new Build();
b.setFirstName("Dick");
b.setLastName("Tracy");
em.persist(b);
代码运行,并且不抛出任何异常。当我检查数据库时,没有任何内容被保留。我是否遗漏了一些东西,或者这是否足以让对象持续存在容器管理的事务中?
I'm using the OpenJPA implementation of JPA 2 and am having problems persisting an object to the database. I want to use transactions managed by the container (Websphere), so my understanding is that, since the transactions are managed, they boilerplate code like tx.begin(), tx.commit(), etc aren't needed.
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.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_2_0.xsd">
<persistence-unit name="buildTest" transaction-type="JTA">
<jta-data-source>jdbc/lfcbuild</jta-data-source>
<class>entities.Build</class>
<properties>
<property name="openjpa.jdbc.Schema" value="APP"/>
<property name="openjpa.TransactionMode" value="managed"/>
<property name="openjpa.ConnectionFactoryMode" value="managed"/>
</properties>
</persistence-unit>
</persistence>
The code I'm executing when persisting a build is this:
EntityManagerFactory emf = Persistence.createEntityManagerFactory("buildTest");
EntityManager em = emf.createEntityManager();
Build b = new Build();
b.setFirstName("Dick");
b.setLastName("Tracy");
em.persist(b);
The code runs, and throws no exceptions. When I check the database, nothing has been persisted. Am I missing something, or should this be enough for the object to persist with container managed transactions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该代码是 EJB 的一部分吗?否则,您可能必须以编程方式启动并提交 Tx。
Is that code part of an EJB? Otherwise you might have to start and commit the Tx programmatically.