无法持久化数据:JTA EntityManager无法使用getTransaction()

时间:2016-03-30 14:14:14

标签: hibernate jpa

在尝试持久化实体时,我得到以下异常。

public class MyClass {

    EntityManagerFactory emFactory;
    EntityManager entityManager;

    public MyClass(){
        emFactory = Persistence.createEntityManagerFactory("persistenceUnit");
        entityManager = emFactory.createEntityManager();
    }

    public void getData() {     
        try {
            TrigData trigData = new TrigData();
            trigData.setKey("abc");     
            entityManager.getTransaction().begin();
            entityManager.persist(trigData);
            entityManager.getTransaction().commit();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


19:31:51,508 ERROR [stderr] (default task-6) java.lang.IllegalStateException: A JTA EntityManager cannot use getTransaction()
19:31:51,508 ERROR [stderr] (default task-6)    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.getTransaction(AbstractEntityManagerImpl.java:1368)
19:31:51,509 ERROR [stderr] (default task-6)    at com.xyz.services.MyClass.getData(MyClass.java:42)
19:31:51,509 ERROR [stderr] (default task-6)    at com.xyz.services.MyClass$Proxy$_$$_WeldClientProxy.getData(Unknown Source)
19:31:51,509 ERROR [stderr] (default task-6)    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
19:31:51,510 ERROR [stderr] (default task-6)    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
19:31:51,510 ERROR [stderr] (default task-6)    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
19:31:51,510 ERROR [stderr] (default task-6)    at java.lang.reflect.Method.invoke(Method.java:606)
19:31:51,510 ERROR [stderr] (default task-6)    at com.sun.jersey.spi.container.JavaMethodInvokerFactory$1.invoke(JavaMethodInvokerFactory.java:60)
19:31:51,511 ERROR [stderr] (default task-6)    at com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$VoidOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:167)
19:31:51,511 ERROR [stderr] (default task-6)    at com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:75)
19:31:51,511 ERROR [stderr] (default task-6)    at com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:302)
19:31:51,512 ERROR [stderr] (default task-6)    at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
19:31:51,512 ERROR [stderr] (default task-6)    at com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:108)
19:31:51,513 ERROR [stderr] (default task-6)    at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)

的persistence.xml:

<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="persistenceUnit" > 
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <jta-data-source>java:jboss/datasources/PostgresDS</jta-data-source>    
        <class>com.xyz.TrigData</class>

        <properties>
            <property name="hibernate.transaction.jta.platform" value="org.hibernate.service.jta.platform.internal.JBossStandAloneJtaPlatform" />
            <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
            <property name="hibernate.hbm2ddl.auto" value="update" />
        </properties>
    </persistence-unit>

</persistence>

1 个答案:

答案 0 :(得分:0)

如果您尝试在Wildfly上部署,请使用EntityManager注释获取@PersistenceContext。所有Wildfly快速入门都有一个实用程序类,@Produces实体管理器将注入其他bean:

public class Resources {
    @Produces
    @PersistenceContext
    private EntityManager em;

    @Produces
    public Logger produceLog(InjectionPoint injectionPoint) {
        return Logger.getLogger(injectionPoint.getMember().getDeclaringClass().getName());
    }

    @Produces
    @RequestScoped
    public FacesContext produceFacesContext() {
        return FacesContext.getCurrentInstance();
    }
}

它会像这样使用:

@Stateless
public class Service {

    @Inject
    private EntityManager em;

有用的交叉引用:How to inject EntityManager in CDI (weld)?