官术网_书友最值得收藏!

Entity listeners

The listeners can be put to use for performing interception on entity life cycle operations. We can also inject CDI managed beans into an entity listener for performing any additional processing. Here are the seven life cycle callback annotations that can be used:

The Task entity has an entity listener associated with it, thus during its life cycle events, the listener callback methods would be invoked. If there is more than one listener, then the invocation is done in the order of the listener declaration:

@Entity
@Table(name="task_detail")
@EntityListeners({TaskAudit.class})
public class Task extends MappedSuperEntity {
// code omitted
}

During each life cycle event of the Task entity, our TaskAudit class methods would be invoked, which are listening to the event. Thus, to listen to the post persist event, the following code declares a trackChanges method with the @PostPersist annotation:

public class TaskAudit {
@PersistenceContext private EntityManager em;
@PostPersist public void trackChanges(MappedSuperEntity entity)
{ ... }
}

While the listeners are handy to use for simple cases, they fall short for handling more complex requirements. There are modules, such as Hibernate Envers, which provide support for auditing capabilities with more capable features, but these are not part of the standard JPA specification.

主站蜘蛛池模板: 汕尾市| 讷河市| 华亭县| 德庆县| 太原市| 元朗区| 福建省| 岫岩| 科尔| 增城市| 舟山市| 尚义县| 织金县| 新宁县| 玉环县| 雷波县| 惠州市| 泸西县| 瑞昌市| 马尔康县| 金坛市| 册亨县| 达拉特旗| 嘉义县| 吉林市| 双桥区| 正蓝旗| 墨脱县| 江山市| 济南市| 宁津县| 嘉兴市| 政和县| 忻州市| 深泽县| 宁武县| 凤台县| 卓资县| 黄浦区| 陇西县| 毕节市|