用于JPA回调的Hibernate事件侦听器 [英] Hibernate event listeners for JPA callbacks
问题描述
如何启用处理JPA回调的Hibernate事件侦听器?
目前我正在使用带有SessionFactory配置的Hibernate 4,但JPA回调没有正常运行,当我坚持一个对象。
任何建议都是最受欢迎的。
源代码
Temp实体类:
package com.esp。实体;
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 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();
$ / code $ / pre
$ b 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>更新< / property>
< property name =hibernate.current_session_context_class>线程< / property>
< mapping class =com.esp.entity.Temp/>
< / session-factory>
< / hibernate-configuration>
程式输出
程式输出为如下所示:
Hibernate:插入TEMP(email,name,roll)值(?,?,?)
对象成功保存
预期输出为:
对象现在会持久
Hibernate:插入TEMP(email,name,roll)值(?,?,?)
Object persist successfully
解决方案 问题基本上也是这样。
事实证明,当你在Hibernate中使用 EntityManager
时(这是可以理解的),这些JPA实体监听器注释才有效。所以如果你想使用这些注释,你应该抛弃 SessionFactory
,并使用JPA投诉 EntityManager
或 EntityManagerFactory
。
我也推荐这种方法,因为如果您尝试使用JPA注释,一个纯粹的JPA解决方案,而不必将自己绑定到一个Hibernate特定的解决方案 - 即 SessionFactory
。
How can I enable the Hibernate event listeners, that handle JPA callbacks?
Currently I am using using Hibernate 4 with SessionFactory configuration, but JPA callbacks are not running properly, when I persist an object.
Any suggestion are most welcome.
Source code
Temp Entity class:
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 class:
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 class:
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();
}
}
The Hibernate configuration:
<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>
Program output
The program output is the following:
Hibernate: insert into TEMP (email, name, roll) values (?, ?, ?)
Object persist successfully
The expected output would be:
Object will persist now
Hibernate: insert into TEMP (email, name, roll) values (?, ?, ?)
Object persist successfully
解决方案 This question basically asked the same.
So it turns out, that these JPA entity listener annotations are only working when you are using EntityManager
in Hibernate (which is understandable). So if you want to use these annotations, you should ditch SessionFactory
, and use the JPA-complaint EntityManager
or EntityManagerFactory
.
I also recommend this approach, because if you are trying to use JPA annotations, it is logical to go for a pure JPA solution, without tying yourself to a Hibernate-specific solution - i.e. SessionFactory
.
这篇关于用于JPA回调的Hibernate事件侦听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!