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

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.

主站蜘蛛池模板: 凤山市| 闽清县| 平乐县| 浦东新区| 利津县| 汕尾市| 尼木县| 千阳县| 双鸭山市| 宁德市| 宜阳县| 仪陇县| 常德市| 巩义市| 南丹县| 田阳县| 大渡口区| 边坝县| 亚东县| 德保县| 桦甸市| 木里| 石棉县| 清丰县| 格尔木市| 会同县| 嫩江县| 德令哈市| 修武县| 遵化市| 镇安县| 渝中区| 察雅县| 易门县| 安国市| 阳信县| 吴川市| 班玛县| 五台县| 绍兴县| 屏南县|