如何在entityManagerFactory中注入Hibernate Interceptor类

时间:2014-02-27 07:03:36

标签: java spring hibernate dependency-injection

我想在我的实体管理器工厂类中注入hibernate Interceptor类。但我找不到任何可以注入hibernate Intercpetor类引用的属性。

实体经理工厂Bean:

<bean id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="jpaVendorAdapter" ref="hibernateJpaVendorAdapter" />
        <property name="packagesToScan" value="com.XXXXX.entity" />
        <property name="jpaProperties">
    </bean>

<!-- Datasource -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
        destroy-method="close">
        <property name="driverClass" value="${jdbc.driver.classname}" />
        <property name="jdbcUrl" value="${jdbc.url}" />
    </bean>

<!-- MyInterceptor bean -->
<bean id="interceptor"  class="com.XXXX.interceptor.AuditInterceptor" "/>

<!-- transaction Manager -->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>

我想了解如何在entityManagerFactory bean ???

中注入我的拦截器bean引用

1 个答案:

答案 0 :(得分:1)

我们不能注入hibernate Intercpetor类的引用实体管理器工厂类。我们可以使用替代或解决方法: 我们可以从EntityManagerFactory bean获取entityManager引用。

EntityManagerFactory entityManagerFactory;

  private static EntityManager entityManager;

  @Autowired
  public void setEntityManagerFactory(EntityManagerFactory entityManagerFactory)
  {
    entityManager = entityManagerFactory.createEntityManager();
    this.entityManagerFactory = entityManagerFactory;
  }

  public Session getCurrentSession()
  {
    return entityManager.unwrap(org.hibernate.Session.class);
  }