javascript
Spring Data JPA 从入门到精通~@EntityListeners注解示例
被@Prepersist注解的方法 ,完成save之前的操作。
被@Preupdate注解的方法 ,完成update之前的操作。
被@PreRemove注解的方法 ,完成remove之前的操作。
被@Postpersist注解的方法 ,完成save之后的操作。
被@Postupdate注解的方法 ,完成update之后的操作。
被@PostRemovet注解的方法 ,完成remove之后的操作。
?This page will provide JPA @EntityListeners example with callbacks @PrePersist, @PostPersist, @PostLoad, @PreUpdate, @PostUpdate, @PreRemove, @PostRemove. JPA?@EntityListeners?is used on entity or mapped superclass at class level. JPA provides callback methods for saving, fetching, updating and removing data from database. Here we will use JPA?EntityManager?to interact with database.
JPA @EntityListeners
@EntityListeners?annotation specifies the callback listener classes . This annotation can be used for an entity or mapped superclass.?
1. To configure single callback listener class, we can do as follows.
2. To configure multiple callback listener classes, we can do as follows.
@EntityListeners({UserListener1.class, UserListener2.class}) public class User { }JPA Callbacks Method
JPA provides callback methods to listen saving, fetching, updating and removing data from database. These callback methods annotated in a listener bean class must have return type?void?and accept?one argument.?
@PrePersist: The method annotated with?@PrePersist?in listener bean class is called before persisting data by entity manager?persist()?method.?
@PostPersist: The method annotated with?@PostPersist?is called after persisting data.?
@PostLoad: The method annotated with?@PostLoad?is called after fetching data using entity manager?find()?method in persistence context or refreshing it with database by using?refresh()?method. If the entity instance is already loaded in persistence context, then calling of?find()?method will not call?@PostLoad.?
@PreUpdate: The method annotated with?@PreUpdate?in listener bean class is called before updating data.?
@PostUpdate: It is called after updating data.?
@PreRemove: The method annotated with?@PreRemove?in listener bean class is called before removing data by using entity manager?remove()?method.?
@PostRemove: It is called after removing data.
Database Schema
For the demo we are using a table with following schema created in MySQL.?
Table: user
Gradle File
Find the gradle file.?
build.gradle
Create Listener Class
Find the listener class which consist callback methods annotated with @PrePersist, @PostPersist, @PostLoad, @PreUpdate, @PostUpdate, @PreRemove and @PostRemove.?
UserListener.java
Create Entity annotated with @EntityListeners
Now find the entity annotated with?@EntityListeners.?
User.java
persistence.xml
Find the persistence.xml file.
<?xml version="1.0" encoding="UTF-8" ?> <persistence xmlns="http://java.sun.com/xml/ns/persistence"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"version="2.0"><persistence-unit name="com.concretepage"><description>JPA Demo</description><provider>org.hibernate.ejb.HibernatePersistence</provider><properties><property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/><property name="hibernate.hbm2ddl.auto" value="update"/><property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/><property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost/concretepage"/><property name="javax.persistence.jdbc.user" value="root"/><property name="javax.persistence.jdbc.password" value=""/></properties></persistence-unit> </persistence>Run Application
First find the JPA utility singleton class that will provide the instance of?EntityManager.?
JPAUtility.java
Find the class to test the application.?
JPAListenerDemo.java
Find the output.
Listening User Pre Persist : Mahesh Listening User Post Persist : Mahesh Listening User Post Load : Mahesh Listening User Pre Update : Krishna Listening User Post Update : Krishna Listening User Pre Remove : Krishna Listening User Post Remove : Krishna總結
以上是生活随笔為你收集整理的Spring Data JPA 从入门到精通~@EntityListeners注解示例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 代换链表入口节点
- 下一篇: Google Pixel 解锁BL、刷入