Persistence API (JPA) series (v) controlling the life cycle of an entity bean

Source: Internet
Author: User


Previous postPersistent API (JPA) series (iv) Manager entitymanager--performing database updateswe explain how to manipulate the database using the various functions of the entity manager.

This article focuses on: control the life cycle of the entity bean.
similar to session beans, entity beans also have their own life cycles, which correspond to different states.

let's begin by explaining the status and lifecycle events of the entity Bean:
1.4 States of entity bean life cycle
2. Event of entity Bean:
@PostLoad
@PrePersist and @postpersist
@PreUpdate and @postupdate
@PreRemove and @postremove.

3. Two methods of event callback:
External callback: Write an external listener.

=============================================================================
1.4 States of entity bean life cycle
entities are divided into containers managed and not managed by the container two kinds. Detection can be done through the two methods of the entity manager:
contains () is used to check whether an entity is managed.
Clear () detaches the entity.



In fact, there are 4 states of entities.
New--New entity: The entity is generated by the app, has no contact with the entity manager, and does not have a unique identifier.
    managed--Persistent entity: The new entity and entity Manager are associated (via persist (), merge (), and so on), exist and managed in the entity manager, and the flag is a unique identifier in the entity manager.
    detached--Detached entity: The entity has a unique identifier, but its identifier is not managed by the entity manager.
    removed--deleted entity: The entity is deleted by the Remove () method, and the corresponding record will be deleted from the database when the current transaction commits.

=============================================================================
2. Callback event for Entity Bean life cycle
when you perform various persistence methods, the state of the entity changes, and the state changes to trigger different life cycle events. These events can use different annotations to indicate the callback function at the time of the occurrence.
@PostLoad after loading
before and after persistence of @PrePersist and @postpersist
before and after @PreUpdate and @postupdate updates
@PreRemove and @postremove Delete before and after

the 7 types of events above correspond to 4 operations of the database.

1) database query
@PostLoad events are triggered in the following situations:
executes the entitymanager.find () or GetReference () method after loading an entity.
after the JPQL query is executed.
after the Entitymanager.refresh () method is called.

2) Database insertion
@PrePersist and @postpersist events occur during the insertion of an entity object into the database:
@PrePersist event occurs immediately after the persist () method is called, and the data is not actually inserted into the database at this time.
@PostPersist event occurs after the data has been inserted into the database.

3) Database Update
triggering of @PreUpdate and @postupdate events is caused by an update entity:
@PreUpdate events are triggered before the state of the entity is synchronized to the database, at which point the data is not actually updated to the database.
@PostUpdate events are triggered after the state of the entity is synchronized to the database, and synchronization occurs when the transaction commits.

4) Database deletion
triggering of @PreRemove and @postremove events is caused by a delete entity:
@PreRemove events are triggered before an entity is deleted from the database, which occurs when the remove () method is called, and the data is not actually deleted from the database.
@PostRemove event is triggered after the entity is deleted from the database.

=============================================================================
3. External callback-write external listener
for the above 7 types of events, we can implement callbacks in two ways.
External Callback: Write an entity listener that implements a callback to an entity bean.
Internal Callback: Callbacks are made by listening within the entity bean.


1) External callback:
A. First write a callback listener class that needs to bind the target entity bean with the @javax.persistence.entitylisteners annotation to implement the monitoring of the entity bean.
B. In this class, you can write multiple callback methods to implement an event interception by adding a different callback specifier before the method.
C. The callback method needs to return a void value and has an object parameter.

This method of writing an external listener, similar to the functionality of AOP, enables the listener of target classes and functions in the system through an external class.
Demo Demos

2) Internal callback steps:
A. The callback method is written in the entity bean. Callback methods written directly in the entity bean do not need to take any parameters.
B. The callback method written directly in the entity Bean does not need to take any parameters.
C. Different callback functions are also annotated with a callback comment.

Demo Demos





Persistence API (JPA) series (v) controlling the life cycle of an entity bean

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.