Java EventListener休眠5
我正在使用Hibernate 5和Spring 4.2.3。我找不到将eventListener添加到SessionFactory范围的方法。我只需要在hibernate持久化对象之前设置一个日期。我在spring.xml中定义了sessionFactoryJava EventListener休眠5,java,spring,hibernate,hibernate-5.x,Java,Spring,Hibernate,Hibernate 5.x,我正在使用Hibernate 5和Spring 4.2.3。我找不到将eventListener添加到SessionFactory范围的方法。我只需要在hibernate持久化对象之前设置一个日期。我在spring.xml中定义了sessionFactory <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"> <property
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mappingResources">
<list>
<value>com/hibernate/mapping/User.hbm.xml</value>
<value>com/hibernate/mapping/PublicKey.hbm.xml</value>
<value>com/hibernate/mapping/File.hbm.xml</value>
<value>com/hibernate/mapping/Url.hbm.xml</value>
<value>com/hibernate/mapping/Role.hbm.xml</value>
<value>com/hibernate/mapping/Operation.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">validate</prop>
<prop key="hibernate.cache.provider_class">org.hibernate.cache.NoCacheProvider</prop>
<prop key="hibernate.c3p0.min_size">5</prop>
<prop key="hibernate.c3p0.max_size">50</prop>
<prop key="hibernate.c3p0.timeout">100</prop>
<prop key="hibernate.c3p0.max_statements">50</prop>
<prop key="hibernate.c3p0.idle_test_period">3000</prop>
<prop key="hibernate.c3p0.validate">true</prop>
</props>
</property>
</bean>
com/hibernate/mapping/User.hbm.xml
com/hibernate/mapping/PublicKey.hbm.xml
com/hibernate/mapping/File.hbm.xml
com/hibernate/mapping/Url.hbm.xml
com/hibernate/mapping/Role.hbm.xml
com/hibernate/mapping/Operation.hbm.xml
org.hibernate.dialogue.mysqldialogue
真的
验证
org.hibernate.cache.NoCacheProvider
5.
50
100
50
3000
真的
我有我的GenericDAOImpl,其中有一个会话工厂:
public abstract class GenericDAOImpl <T, PK extends Serializable> implements GenericDAO<T, PK> {
private SessionFactory sessionFactory;
/** Domain class the DAO instance will be responsible for */
protected Class<T> type;
@SuppressWarnings("unchecked")
public GenericDAOImpl() {
Type t = getClass().getGenericSuperclass();
ParameterizedType pt = (ParameterizedType) t;
type = (Class<T>) pt.getActualTypeArguments()[0];
}
@SuppressWarnings("unchecked")
public PK create(T o) {
return (PK) getSession().save(o);
}
public T read(PK id) {
return (T) getSession().get(type, id);
}
public void update(T o) {
getSession().update(o);
}
public void delete(T o) {
getSession().delete(o);
}
@SuppressWarnings("unchecked")
public List<T> getAll() {
return getSession().createCriteria(type).list();
}
protected Session getSession() {
return sessionFactory.getCurrentSession();
}
// getters and setters
}
公共抽象类GenericDAOImpl实现GenericDAO{
私人会话工厂会话工厂;
/**DAO实例将负责的域类*/
保护类类型;
@抑制警告(“未选中”)
公共通用DAOImpl(){
类型t=getClass().getGenericSuperclass();
参数化类型pt=(参数化类型)t;
类型=(类)pt.getActualTypeArguments()[0];
}
@抑制警告(“未选中”)
公共PK创建(TO){
返回(PK)getSession().save(o);
}
公共T读(主键id){
return(T)getSession().get(type,id);
}
公开作废更新(TO){
getSession().update(o);
}
公共作废删除(TO){
getSession()。删除(o);
}
@抑制警告(“未选中”)
公共列表getAll(){
返回getSession().createCriteria(type).list();
}
受保护会话getSession(){
返回sessionFactory.getCurrentSession();
}
//接球手和接球手
}
我已经看到了几种方法,但其中一些方法不适用于Hibernate 5(如积分器
)。我找不到通过xml将其添加到eventListener的方法,或者只是在sessionFactory范围中添加代码。我发现了如何使用sessionFactory.withOptions().eventListeners(监听器)添加到会话范围代码>
谢谢 最后,非常简单,我找到了一种通过xml添加拦截器的方法,只需添加:
<property name="entityInterceptor">
<bean class="com.xxx.xxx.className"></bean>
</property>
我在帖子里找到了这个:
您尝试过拦截器吗@Hernandez我在eventListener之前尝试过这种可能性,但是我没有找到通过xml将拦截器添加到SessionFactory的方法。我知道使用代码就像newconfiguration().setInterceptor(newauditinterceptor())
一样简单。但是我已经用xml定义了我的sessionFactory。我从未尝试过,但我想您可以创建一个扩展org.springframework.orm.hibernate5.LocalSessionFactoryBean的类,在构造函数中添加Configuration().setInterceptor(…),然后在xml配置文件中使用它。@MarcoA.Hernandez我刚刚找到了一个解决方案。谢谢