mysql事件侦听器_用于JPA回调的Hibernate事件侦听器

如何启用处理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配置:

com.mysql.jdbc.Driver

jdbc:mysql://localhost:3306/demo

root

root

org.hibernate.dialect.MySQLDialect

true

update

thread

程序输出

程序输出如下:

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值