Use Apache OPENJPA to develop EJB 3.0 applications, part 6th: Callback for handling entity lifecycle events

Source: Internet
Author: User

In the enterprise application development process, there are often such requirements: when some data in the enterprise application is added, deleted and modified, some specific actions are initiated to fulfill some special requirements in the enterprise application, such as the enterprise application to complete the data operation log, the processing data of a relationship, or the completion of a Some local statistical work and so on. Typically, developers have two options:

Developers provide independent code to handle this requirement;

Using the "trigger" technique in a relational database, the developer is assigned to specify actions to be added, deleted, and modified in a particular table to complete processing of the data in the database.

However, these two methods have certain limitations, in the 1th way, the special design of the code and the main program of the coupling between the high, can not be independent maintenance, it is difficult to reuse; The 2nd approach is only applicable to the development of relational database, and the way of development is limited.

OpenJPA provides another way to deal with this particular requirement, the callback method. The callback method can monitor the entire lifecycle of the entity, and can easily join the developer's own code at all times of the lifecycle to address the specific needs of the actual business. The entity lifecycle currently supported in OpenJPA includes: Before entity persistence, entities can be persisted, entities are loaded, entity state is written to the database, entity state is written to the database, before the entity is deleted, and after the entity is deleted.

The callback method in OpenJPA can be implemented on two levels:

To define a callback method in an entity class

Developers write the processing methods that match the actual business requirements in the entity classes, registering them in the entity lifecycle Monitoring mechanism with annotations, and when the corresponding lifecycle events of the entity are triggered, these methods are invoked to meet the specific business needs of the user. This approach applies to situations where the callback method is not too much and the business is not complex, and this is not recommended as a programming method.

Providing listeners for entity classes

In addition to defining callback methods in an entity class, a developer can relate the lifecycle events of an entity to the Java method, using the entity listener, which uses a listener mechanism similar to that of AWT or Swing. The developer provides the entity listener, then registers the listeners with the appropriate entity, and the entity becomes the source of the event. When an entity lifecycle event is triggered, these registered entity listeners are activated individually. With entity listeners, you can implement the listener's inheritance, sharing, and reuse, and therefore can be applied to a business environment that is more complex than simply using the callback method.

Entity life cycle Related annotations

OpenJPA can provide callback support for multiple phases of the entity lifecycle, including before the entity is persisted, when persisted, when initialized, and so on. Each phase of the entity lifecycle has a corresponding callback method annotation in JPA, which can be used in the listener for entity classes or entity classes, and developers use these annotations to assign methods that are called in the entity classes when the callback occurs.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.