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

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.

主站蜘蛛池模板: 德格县| 连州市| 遂平县| 乐亭县| 山西省| 阳城县| 辉南县| 通化市| 体育| 洛南县| 资兴市| 扶余县| 攀枝花市| 上饶县| 姜堰市| 南川市| 灵山县| 静安区| 顺平县| 资中县| 江孜县| 卫辉市| 青河县| 沂水县| 安国市| 大埔县| 玉树县| 建平县| 郁南县| 班玛县| 平山县| 红原县| 玛纳斯县| 南康市| 安岳县| 尖扎县| 姚安县| 清涧县| 凤山市| 河池市| 千阳县|