我正在尝试使用骆驼 jpa 组件将实体写入数据库。问题是我得到
javax.persistence.TransactionRequiredException: no transaction is in progress
at org.hibernate.internal.SessionImpl.checkTransactionNeeded(SessionImpl.java:3428)
at org.hibernate.internal.SessionImpl.doFlush(SessionImpl.java:1395)
at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1391)
at org.apache.camel.component.jpa.JpaProducer$1.doInTransaction(JpaProducer.java:85)
解析持久单元并创建表。这里是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="pu" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<properties>
<property name="hibernate.archive.autodetection" value="class" />
<property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect" />
<property name="hibernate.connection.driver_class" value="oracle.jdbc.driver.OracleDriver" />
<property name="hibernate.connection.url" value="jdbc:oracle:thin:@localhost:1521:xe" />
<property name="hibernate.connection.username" value="user" />
<property name="hibernate.connection.password" value="pass" />
<property name="hibernate.hbm2ddl.auto" value="update" />
</properties>
</persistence-unit>
</persistence>
我创建了一个测试来编写一个实体:
/**
* Created by moritz on 16.09.2016.
*/
public class JpaTest extends CamelTestSupport {
@EndpointInject(uri = "mock:out")
protected MockEndpoint out;
@EndpointInject(uri = "direct:in")
protected DirectEndpoint in;
@EndpointInject()
@Test
public void writeSome() throws Exception {
PartnerFactaEntity e = new PartnerFactaEntity();
e.setNameIrs("huhu");
e.setPartnerId("1");
e.setUsDokumentGueltigBis(new Date());
e.setUsDokumentGueltigVon(new Date());
context.start();
ProducerTemplate template = context.createProducerTemplate();
template.sendBody(in, e);
}
@Before
public void setup() throws Exception {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("pu");
// ( (JndiRegistry) context.getRegistry() ).bind(
// "EntityManagerFactory", emf );
// ( (JndiRegistry) context.getRegistry() ).bind( "TransactionManager",
// new JpaTransactionManager() );
JpaComponent jpaComponent = context.getComponent("jpa", JpaComponent.class);
jpaComponent.setEntityManagerFactory(emf);
JpaTransactionManager taM = new JpaTransactionManager();
taM.setEntityManagerFactory( emf );
jpaComponent.setTransactionManager( taM );
context.addRoutes(new TestRoute());
}
@Override
public boolean isUseRouteBuilder() {
return false;
}
private class TestRoute extends RouteBuilder {
@Override
public void configure() throws Exception {
from(in).to("jpa:PartnerFactaEntity").to(out);
}
}
}
由于没有真正的方法来开始和结束骆驼语义中的事务,我认为问题与设置有关?
那么如何解决这个问题呢?
编辑
我听从克劳斯的建议并添加.transacted()
了路线。这产生No bean could be found in the registry of type: PlatformTransactionManager
然后我将此行添加到设置方法:
((JndiRegistry)( (PropertyPlaceholderDelegateRegistry) context.getRegistry()).getRegistry()).bind( "PlatformTransactionManager", taM );
这又产生了javax.persistence.TransactionRequiredException: no transaction is in progress
。
我不确定在 JpaComponent 上配置什么以及将什么绑定到注册表中。
同时我调试了一下。我检查了 JpaProducer 中entityManager.flush()
抛出异常的字段:trasactionTemplate 设置为“PRPAGATION_REQUIRED,ISOLATION_DEFAULT”,并带有正确的 JpaTransactionManager。