org.apache.openjpa.persistence.InvalidStateException:此代理未配置为使用托管事务

时间:2015-10-09 17:43:48

标签: java hibernate jpa apache-camel openjpa

我正在尝试配置camel jpa组件,当我运行测试用例时,我收到此错误

“org.apache.openjpa.persistence.InvalidStateException:此代理未配置为使用托管交易。”

但是如果我没有使用任何事务管理器,它将采用默认值,并且一切顺利

我的配置

`@JndiBind("jpa")
@Provides*/
@Singleton
public JpaComponent getJpa(JpaVendorAdapter vendorAdapter){
    JpaComponent jpa = new JpaComponent();
    LocalEntityManagerFactoryBean fBean = new LocalEntityManagerFactoryBean();
    fBean.setJpaVendorAdapter(vendorAdapter);
    fBean.setPersistenceUnitName("camel");
    fBean.afterPropertiesSet();
    EntityManagerFactory entityManagerFactory = fBean.getNativeEntityManagerFactory();
    JpaTransactionManager txMgr = new JpaTransactionManager();
    txMgr.setEntityManagerFactory(entityManagerFactory);

    jpa.setEntityManagerFactory(entityManagerFactory);

    jpa.setTransactionManager(txMgr);

    return jpa;
}`

1 个答案:

答案 0 :(得分:1)

问题在于在代码中获取EntityManager的对象。

    EntityManagerFactory entityManagerFactory = fBean.getObject();

    JpaTransactionManager txMgr = new JpaTransactionManager();
    txMgr.setEntityManagerFactory(entityManagerFactory);
    txMgr.afterPropertiesSet();

    jpa.setEntityManagerFactory(entityManagerFactory);
    jpa.setTransactionManager(txMgr);