我正在尝试配置骆驼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;
}`
最佳答案
问题在于在代码中获取EntityManager的对象。
EntityManagerFactory entityManagerFactory = fBean.getObject();
JpaTransactionManager txMgr = new JpaTransactionManager();
txMgr.setEntityManagerFactory(entityManagerFactory);
txMgr.afterPropertiesSet();
jpa.setEntityManagerFactory(entityManagerFactory);
jpa.setTransactionManager(txMgr);