内容简介:当您需要将自己的操作方法绑定到某个实体生命周期事件时,请不要重新发明轮子。只需依靠内置的JPA回调。本应用是启用JPA回调(Pre/ PostPersist,Pre/ PostUpdate,Pre/ PostRemove,和PostLoad)的示例。第一步:在实体中,编写回调方法并使用正确的注释:
当您需要将自己的操作方法绑定到某个实体生命周期事件时,请不要重新发明轮子。只需依靠内置的JPA回调。
本应用是启用JPA回调(Pre/ PostPersist,Pre/ PostUpdate,Pre/ PostRemove,和PostLoad)的示例。
第一步:在实体中,编写回调方法并使用正确的注释:
@Entity
<b>public</b> <b>class</b> User implements Serializable {
<b>private</b> <b>static</b> <b>final</b> Logger logger =
Logger.getLogger(User.<b>class</b>.getName());
<b>private</b> <b>static</b> <b>final</b> <b>long</b> serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
<b>private</b> Long id;
<b>private</b> String name;
<b>private</b> String city;
<b>private</b> <b>int</b> age;
@PrePersist
<b>public</b> <b>void</b> prePersist() {
logger.info(<font>"@PrePersist callback ..."</font><font>);
}
@PreUpdate
<b>public</b> <b>void</b> preUpdate() {
logger.info(</font><font>"@PreUpdate callback ..."</font><font>);
}
@PreRemove
<b>public</b> <b>void</b> preRemove() {
logger.info(</font><font>"@PreRemove callback ..."</font><font>);
}
@PostLoad
<b>public</b> <b>void</b> postLoad() {
logger.info(</font><font>"@PostLoad callback ..."</font><font>);
}
@PostPersist
<b>public</b> <b>void</b> postPersist() {
logger.info(</font><font>"@PostPersist callback ..."</font><font>);
}
@PostUpdate
<b>public</b> <b>void</b> postUpdate() {
logger.info(</font><font>"@PostUpdate callback ..."</font><font>);
}
@PostRemove
<b>public</b> <b>void</b> postRemove() {
logger.info(</font><font>"@PostRemove callback ..."</font><font>);
}
</font>
注意:在bean类上注释的回调方法必须返回void并且不带参数,比如@PrePersist 到@PostRemove
源代码可以在 这里 找到
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:- 流程篇 | 避免 evalString 进行回调,使用 JSB 进行手动绑定
- event-promise:让事件回调,更容易使用 Promise
- 【Cocos Creator】拒绝 evalString 进行回调,使用 JSB 进行手动绑定(流程篇)
- 转译:使用react hooks优化回调函数在组件间的传递,useState,useReducer?
- [OC] 关于block回调、高阶函数“回调再调用”及项目实践
- 即使回调IsOneWay,WCF客户端也会因回调而死锁
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Tales from Facebook
Daniel Miller / Polity Press / 2011-4-1 / GBP 55.00
Facebook is now used by nearly 500 million people throughout the world, many of whom spend several hours a day on this site. Once the preserve of youth, the largest increase in usage today is amongst ......一起来看看 《Tales from Facebook》 这本书的介绍吧!