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

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

问题描述

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



POJO:

  @Entity 
@EntityListner(AuditLogging.class)
class用户
{
//属性的获取/设置
}

AuditLogging(Listner类)

 public class AuditInterceptor 
{

@PersistenceContext
EntityManager entityManager;

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

@PrePersist
public void prePersist(Object obj)
{
//这里我想使用ENTITY管理器对象,以便执行CRUD操作
//带有prePersist对象。

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

//但是我得到了实体管理器对象


$ b的NULL POINTER EXCEPTION $ b

}



JDBC-CONFIg.xml

  < bean id =entityManagerFactory
class =org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean>
< property name =dataSourceref =dataSource/>
< property name =jpaVendorAdapterref =hibernateJpaVendorAdapter/>
< property name =packagesToScanvalue =com.XXXXX.entity/>
< property name =jpaProperties>
< / bean>

<! - 数据源 - >
< bean id =dataSourceclass =com.mchange.v2.c3p0.ComboPooledDataSource
destroy-method =close>
< property name =driverClassvalue =$ {jdbc.driver.classname}/>
< property name =jdbcUrlvalue =$ {jdbc.url}/>
< / bean>


<! - transaction manager - >
< bean id =transactionManagerclass =org.springframework.orm.jpa.JpaTransactionManager>
< property name =entityManagerFactoryref =entityManagerFactory/>
< / bean>

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

解决方案
  @Autowired 
EntityManagerFactory entityManagerFactory;

private static EntityManager entityManager;

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

$ / code>

以下是我们需要牢记的几点注意事项:

  1。我们不能将EntityManager注入EntityListener(通过@presistenceContext)。 EntityListener 
不受任何容器
的管理2. @presistenceContext类不能是静态的。所以我们不能在类加载时获得实例。
3. EntityListeners由JPA实例化,所以Spring没有机会注入EntityManager


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

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

解决方案

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. We can't inject an EntityManager into an EntityListener(through @presistenceContext). EntityListener
is not managed by any of the containers
 2. @presistenceContext 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天全站免登陆