事务和JPA / Persistence.xml

时间:2014-06-29 14:30:39

标签: java spring hibernate jpa apache-camel

我在使用Hibernate的JPA实现时遇到问题(我正在进行Camel Tracer example

我正在使用Hibernate JPA实现:

<dependency>
  <groupId>org.hibernate</groupId>
  <artifactId>hibernate-entitymanager</artifactId>
  <version>4.3.5.Final</version>
</dependency>

如果我在META-INF文件夹中包含persistence.xml,一切正常:

<persistence xmlns="http://java.sun.com/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             version="1.0">
  <persistence-unit name="tracer" transaction-type="RESOURCE_LOCAL">
    <class>org.apache.camel.processor.interceptor.jpa.JpaTraceEventMessage</class>
    <properties>
      <property name="hibernate.dialect" value="..."/>
      <property name="hibernate.connection.driver_class" value="..."/>
      <property name="hibernate.connection.url" value="..."/>
      <property name="hibernate.hbm2ddl.auto" value="create"/>
      <property name="hibernate.connection.username" value="..." />
      <property name="hibernate.connection.password" value="..."/>
    </properties>
  </persistence-unit>
</persistence>

我想使用Java方式,所以我删除了persistence.xml并创建了以下bean:

@Configuration
@EnableTransactionManagement
public class AppConfiguration
{
      @Bean public EntityManagerFactory entMngFac()
      {
        EntityManagerFactory emf  = Persistence.createEntityManagerFactory("tracer");
        return emf;
      }
      @Bean public DataSource ds()
      {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName("...");
        dataSource.setUrl("...");
        dataSource.setUsername( "..." );
        dataSource.setPassword( "..." );
        return dataSource;
      }
      @Bean public PlatformTransactionManager ptm(EntityManagerFactory emf, DataSource ds)
      {
        JpaTransactionManager jpat = new JpaTransactionManager();
        jpat.setDataSource(ds);
        jpat.setEntityManagerFactory(emf);
        return jpat;
      }
      @Bean public TransactionTemplate tranTemp(PlatformTransactionManager ptm)
      {
        TransactionTemplate tt = new TransactionTemplate();
        tt.setTransactionManager(ptm);
        return tt;
      }
}

保存后,当Camel尝试刷新它创建的JPATracer对象时,我得到以下异常:

javax.persistence.TransactionRequiredException: no transaction is in progress
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.checkTransactionNeeded(AbstractEntityManagerImpl.java:1171)
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:1332)
    at org.apache.camel.component.jpa.JpaProducer$1.doInTransaction(JpaProducer.java:86)

由于“没有事务正在进行中”错误来自“doInTransaction”方法,这有点奇怪。

我的想法是Camel正在启动一个事务,然后Hibernate试图刷新对象并且不知道Camel已启动的事务?所以在某个地方有一些混合的交易,但我无法弄清楚在哪里。

1 个答案:

答案 0 :(得分:3)

尝试使用以下EntityManagerFactory配置(请注意packagesToScan调用)

@Bean
public EntityManagerFactory entityManagerFactory() {
   LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
   em.setDataSource(ds());
   em.setPackagesToScan(new String[] { "org.apache.camel.processor.interceptor.jpa" });
   JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
   em.setJpaVendorAdapter(vendorAdapter);
   return em.getObject();
}