如何启用处理JPA回调的Hibernate事件侦听器?
目前我正在使用Hibernate 4和SessionFactory配置,但是当我持久化对象时,JPA回调没有正常运行。
欢迎任何建议。
临时实体类:
package com.esp.entity;
import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.PostLoad;
import javax.persistence.Table;
import com.esp.aaa.TempVal;
@Entity
@EntityListeners(value=TempVal.class)
@Table(name="TEMP")
public class Temp {
private int id;
private String name;
private String email;
private int roll;
@Id @GeneratedValue
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public int getRoll() {
return roll;
}
public void setRoll(int roll) {
this.roll = roll;
}
@PostLoad
public void load(){
System.out.println("post load called here");
}
}
TempVal类:
package com.esp.aaa;
import javax.persistence.PrePersist;
public class TempVal {
@PrePersist
public void validate(Object temp){
System.out.println("Object will persist now");
}
}
MainClass类:
package com.esp.aaa;
import org.hibernate.Session;
import com.esp.entity.Temp;
import com.esp.utility.HibernateUtils;
public class MainClass {
public static void main(String args[]) {
HibernateUtils.createSessionFactory();
Session session=HibernateUtils.getSessionFactory().getCurrentSession();
session.beginTransaction();
Temp temp=new Temp();
temp.setEmail("abc@gmail.com");
temp.setName("Lucky");
temp.setRoll(1112);
session.save(temp);
System.out.println("Object persist successfully");
session.getTransaction().commit();
HibernateUtils.shutdown();
}
}
Hibernate配置:
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/demo</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<property name="hibernate.current_session_context_class">thread</property>
<mapping class="com.esp.entity.Temp"/>
</session-factory>
</hibernate-configuration>
程序输出如下:
Hibernate: insert into TEMP (email, name, roll) values (?, ?, ?)
Object persist successfully
预期输出为:
Object will persist now
Hibernate: insert into TEMP (email, name, roll) values (?, ?, ?)
Object persist successfully
答案 0 :(得分:7)
这question基本上也问了同样的问题。
事实证明,这些JPA实体监听器注释仅在Hibernate中使用EntityManager
时才有效(这是可以理解的)。因此,如果您想使用这些注释,则应该放弃SessionFactory
,并使用JPA投诉EntityManager
或EntityManagerFactory
。
我也推荐这种方法,因为如果你试图使用JPA注释,那么选择纯粹的JPA解决方案是合乎逻辑的,而不必将自己与特定于Hibernate的解决方案联系起来 - 即SessionFactory
。
答案 1 :(得分:6)
添加一个名为 org.hibernate.integrator.spi.Integrator 的文件,其中包含一行
org.hibernate.jpa.event.spi.JpaIntegrator
在您的META-INF / services文件夹中。这将为基于sessionFactory的配置启用JPA生命周期注释,包括@EntityListeners。