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

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.

主站蜘蛛池模板: 法库县| 将乐县| 古浪县| 方山县| 营山县| 耿马| 苍梧县| 宣化县| 常山县| 咸丰县| 郸城县| 马尔康县| 伊金霍洛旗| 荣成市| 常宁市| 丰顺县| 郴州市| 珲春市| 云和县| 开原市| 左权县| 甘肃省| 新蔡县| 磐安县| 柏乡县| 乐山市| 台州市| 黄陵县| 仪征市| 桃园市| 慈利县| 长顺县| 明水县| 乐至县| 卢龙县| 临沭县| 平南县| 合阳县| 拜城县| 古交市| 贵定县|