如何在 EntityListeners 中注入 EntityManager [英] How to inject EntityManager in EntityListeners

查看:25
本文介绍了如何在 EntityListeners 中注入 EntityManager的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在 EntityListener 类中注入 EntityManager 以便我可以对其执行 CRUD 操作.

I need to inject EntityManager in EntityListener class so that I can perform CRUD operation on it.

POJO:

@Entity
@EntityListner(AuditLogging.class)
class User
{
      //Getter / setter of properties
}

AuditLogging(Listner 类)

AuditLogging (Listner class)

public class AuditInterceptor
{

  @PersistenceContext
  EntityManager entityManager;

  public void setEntityManager(EntityManager entityManager)
  {
    this.entityManager = entityManager;
  }

  @PrePersist
  public void prePersist(Object obj)
  {
     // Here I want to use ENTITY manager object so that I can perform CRUD operation
     // with prePersist coming object.

      entityManager.unwrap(Session.class).save(obj);

     // But I am getting NULL POINTER EXCEPTION for entity manager object 
   }

}

JDBC-CONFIg.xml

JDBC-CONFIg.xml

<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>


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

EntityListener 不受任何容器的管理.EntityListener 是由 JPA 实例化的,因此 Spring 没有机会注入 EntityManager.我的问题是,我们如何在 EntityListener 类中注入 EntityManager 以便我可以对其执行 CRUD 操作???

EntityListener is not managed by any of the container.EntityListeners are instanciated by JPA, so Spring does not have an opportunity to inject EntityManager. My question is, how we can inject inject EntityManager in EntityListener class so that I can perform CRUD operation on it ???

推荐答案

无论如何,我是通过从 EntityManagerFactory bean 中获取 entityManager 引用来完成的,该 bean 是在我的 jdbc-config.xml 中配置的.但这又不是我想要的.我想用 @PersistenceContext 解决.

Anyways, I got this done by getting entityManager reference from EntityManagerFactory bean which is configured in my jdbc-config.xml. But again this is not what I wanted. I wanted to work around with @PersistenceContext.

  @Autowired
  EntityManagerFactory entityManagerFactory;

  private static EntityManager entityManager;

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

以下是一些我们需要牢记的注意事项:

Here are few notes that we need to keep in mind:

  1. 我们不能将 EntityManager 注入到 EntityListener(通过@PersistenceContext).EntityListener 不受任何容器
  2. @PersistenceContext 类不能是静态的.所以我们不能在类加载时获取实例.
  3. EntityListeners 是由 JPA 实例化,因此 Spring 没有机会注入 EntityManager
  1. We can't inject an EntityManager into an EntityListener (through @PersistenceContext). EntityListener is not managed by any of the containers
  2. @PersistenceContext class cannot be static. So we cant attain the instance while class loading.
  3. EntityListeners are instantiated by JPA, so Spring does not have an opportunity to inject EntityManager

这篇关于如何在 EntityListeners 中注入 EntityManager的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆