0
這是我第一次真正接觸到JPA,我試圖運行最簡單的更新語句。我在使用Hibernate作爲JPA實現的jBoss 7.1.x服務器中運行。任何幫助將不勝感激..如何更新JPA中的實體
這裏是我的EntityManager的生產方法:
@ApplicationScoped
public class EntityManagerProducer
{
@PersistenceContext
private EntityManager entityManager;
@Produces
@RequestScoped
public EntityManager getEntityManager()
{
return entityManager;
}
}
這裏是試圖執行更新的DAO方法:
@Inject EntityManager em;
public void updateRequestStatus(String requestNumber, String newStatus)
{
em.getTransaction().begin();
ServiceRequestEntity serviceRequestToUpdate = em.find(RequestEntity.class, requestNumber);
requestToUpdate.setStatus(newStatus);
em.merge(serviceRequestToUpdate);
em.getTransaction().commit();
}
這裏是持久性。 xml:
<?xml version="1.0" encoding="UTF-8"?>
<persistence 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"
version="2.0">
<persistence-unit name="database" transaction-type="RESOURCE_LOCAL">
<jta-data-source>java:/jdbc/myDS</jta-data-source>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="false" />
<property name="hibernate.use_sql_comments" value="true" />
</properties>
</persistence-unit>
</persistence>
這裏是錯誤信息(可以提供完整的stacktrace if需要在「合併」線):
javax.persistence.TransactionRequiredException: JBAS011469: Transaction is required to perform this operation (either use a transaction or extended persistence context)
....
at org.jboss.weld.proxies.EntityManager$-1727851269$Proxy$_$$_WeldClientProxy.merge(EntityManager$-1727851269$Proxy$_$$_WeldClientProxy.java) [weld-core-1.1.5.AS71.Final.jar:]
作爲一個方面說明,我想你可能想刪除EntityManagerProducer的@Produces註解,如對你想要做的事情可能沒有必要。 – 2013-02-27 05:17:33