user3029929
user3029929

Reputation: 491

How to inject Hibernate Interceptor class in entityManagerFactory

I want to inject hibernate Interceptor class in my entity manager factory class. But I couldn't find any property in which I can inject hibernate Intercpetor class reference.

Entity Manager Factory 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>

I want to understand how I can inject my interceptor bean reference in entityManagerFactory bean???

Upvotes: 1

Views: 733

Answers (1)

user3029929
user3029929

Reputation: 491

We cannot inject hibernate Intercpetor class reference entity manager factory class. We can use alternate or work around: We can get entityManager reference from EntityManagerFactory bean.

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);
  }

Upvotes: 1

Related Questions