我使用Spring Boot 1.3.2和Spring Data JPA 1.9.4与MS Sql server2014数据库。
I have one table with entity Book and table as below.
@Table(name="Book")
@Entity
@EntityListeners (HibernateListener.class)
public class Book{
@Column(name="ID", nullable=false)
@Id private Integer id;
@Column(name="Name", nullable=false)
private String name;
//getters and setters
}
public class HibernateListener{
/**
* automatic property set before any database persistence
*/
@PostUpdate
public void postUpdate(Cat o) {
System.out.println("Post update")
}
}
As per the requirement i need to do some operations after update object i.e post updation of cat object only. so i added HibernateListener and added one method postupdate with annotation of @PostUpdate. But this call back method calling on every time with & with out updation of cat object by using JPA saveandflush(cat) method.
Is any thing missed from my side to restrict to calling this call back method only when updates happens on cat object?
暂时我以不同的方式尝试解决上述问题。
我尝试通过实现PostUpdateEventListner方法覆盖onPostUpdate(PostUpdateEvent事件)来创建新类,并在启动spring boot时注册此侦听器。但这个覆盖的方法也onPostUpdate(PostUpdateEvent事件)每次调用on&通过使用JPA saveandflush(cat)方法更新cat对象。
请建议只有在cat对象上发生更新时才限制调用此回调方法的好方法吗?