gpt4 book ai didi

spring - 在 Hibernate 的 EmptyInterceptor 中注入(inject) JPA 的实体管理器

转载 作者:行者123 更新时间:2023-12-04 04:05:41 33 4
gpt4 key购买 nike

我在我的数据访问层中使用 JPA-2.0 和 Hibernate。

出于审计日志记录的目的,我通过在 persistence.xml 中配置以下属性来使用 Hibernate 的 EmptyInterceptor:

<property name="hibernate.ejb.interceptor"  
value="com.mycom.audit.AuditLogInterceptor" />

在哪里 审计日志拦截器 扩展 hibernate 的' org.hibernate.EmptyInterceptor '。
public class AuditLogInterceptor extends EmptyInterceptor {  

private Long userId;

public AuditLogInterceptor() {}

@Override
public boolean onSave(Object entity, Serializable id, Object[] state,
String[] propertyNames, Type[] types) throws CallbackException {
// Need to perform database operations using JPA entity manager
return false;
}

@Override
public boolean onFlushDirty(Object entity, Serializable id,
Object[] currentState, Object[] previousState,
String[] propertyNames, Type[] types) {
// other code here
return false;
}

@Override
public void postFlush(Iterator iterator) throws CallbackException {
System.out.println("I am on postFlush");
// other code here
}
}

我在数据访问层中使用 JPA 实体管理器来执行数据库操作。 JPA 配置如下:
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
p:persistenceUnitName="PersistenceUnit"
p:persistenceXmlLocation="classpath*:persistence.xml"
p:dataSource-ref="dataSource" p:jpaVendorAdapter-ref="jpaAdapter">
<property name="loadTimeWeaver">
<bean
class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" />
</property>
</bean>

我的 AbstractDAO 是:
public class AbstractDao<T, ID extends Serializable> {

private final transient Class<T> persistentClass;

protected transient EntityManager entityManager;

@SuppressWarnings("unchecked")
public AbstractDao() {

this.persistentClass = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
}

@PersistenceContext
public final void setEntityManager(final EntityManager entityMgrToSet) {

this.entityManager = entityMgrToSet;
}

public final Class<T> getPersistentClass() {

return persistentClass;
}

public final void persist(final T entity) {

entityManager.persist(entity);
}

}

我想在“AuditLogInterceptor”中注入(inject) JPA 实体管理器,这样我就可以像我的抽象 DAO 一样在“AuditLogInterceptor”中执行数据库操作。

任何的想法?正确的解决方案应该是什么?

最佳答案

我有一种在“AuditLogInterceptor”中使用 JPA Entity Manager 执行数据库操作的简单方法

我创建了下面的类,它将提供应用程序上下文引用:

@Component("applicationContextProvider")
public class ApplicationContextProvider implements ApplicationContextAware {
private static ApplicationContext context;

public static ApplicationContext getApplicationContext() {
return context;
}

@Override
public void setApplicationContext(ApplicationContext ctx) {
context = ctx;
}
}

创建数据访问类:
@Repository("myAuditDAO")
public class myAuditDAO<T, ID extends Serializable> {

private final transient Class<T> persistentClass;

protected transient EntityManager entityManager;

@SuppressWarnings("unchecked")
public MyDAO() {

this.persistentClass = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
}

@PersistenceContext
public final void setEntityManager(final EntityManager entityMgrToSet) {

this.entityManager = entityMgrToSet;
}

public final Class<T> getPersistentClass() {

return persistentClass;
}

public final T findById(final ID theId) {

return entityManager.find(persistentClass, theId);
}

public final void persist(final T entity) {

entityManager.persist(entity);
}

public final void merge(final T entity) {

entityManager.merge(entity);
}
}

并在“AuditLogInterceptor”中使用“ApplicationContextProvider”来获取“MyAuditDAO”的引用,该引用将 JPA 实体管理器作为在 DAO 初始化期间注入(inject)的属性。现在在“MyAuditDAO”的帮助下,我可以执行数据库操作。
public class AuditLogInterceptor extends EmptyInterceptor {  

@Override
public void postFlush(Iterator iterator) throws CallbackException {

// Here we can get the MyAuditDao reference and can perform persiste/merge options
MyAuditDao myAuditDao = (MyAuditDao ) ApplicationContextProvider.getApplicationContext().getBean("myAuditDao");

// myAuditDao.persist(myEntity);

}
}

关于spring - 在 Hibernate 的 EmptyInterceptor 中注入(inject) JPA 的实体管理器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19217872/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com