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

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.

主站蜘蛛池模板: 清河县| 连南| 来安县| 保山市| 林西县| 江阴市| 若尔盖县| 蒙城县| 巴林左旗| 满城县| 绿春县| 日照市| 汽车| 邹城市| 鹤山市| 西林县| 邵武市| 通山县| 灌阳县| 安丘市| 遵义县| 奉节县| 台南县| 濮阳市| 靖远县| 襄垣县| 平南县| 湄潭县| 自贡市| 皋兰县| 龙江县| 长泰县| 昔阳县| 合阳县| 屏东县| 利川市| 珲春市| 辽宁省| 玉龙| 武平县| 卢氏县|