The below example will show ,if there is any operation in a entity then how we will get notification in JPA+Hibernate .
Note: If the operation is in only application not in backend operation (like update,delete,add query in oracle developer directly)
While defining Entity class need to add below annotation .
@EntityListeners(MyEntityListener.class)
We will discuss about MyEntityListener.class also.
Example of Entity class :
@Entity
@EntityListeners(MyEntityListener.class)
public class MyEntity {
@Id
@GeneratedValue
private int id;
private String msg;
public MyEntity() { }
public MyEntity(String msg) {
this.msg = msg;
}
.............
}
Listner class:
In this class we will get notification if there is any operation in entity .
Example:
public class MyEntityListener {
@PrePersist
void onPrePersist(MyEntity myEntity) {
System.out.println("MyEntityListener.onPrePersist(): " + myEntity);
}
@PostPersist
void onPostPersist(MyEntity myEntity) {
System.out.println("MyEntityListener.onPostPersist(): " + myEntity);
}
@PostLoad
void onPostLoad(MyEntity myEntity) {
System.out.println("MyEntityListener.onPostLoad(): " + myEntity);
}
@PreUpdate
void onPreUpdate(MyEntity myEntity) {
System.out.println("MyEntityListener.onPreUpdate(): " + myEntity);
}
@PostUpdate
void onPostUpdate(MyEntity myEntity) {
System.out.println("MyEntityListener.onPostUpdate(): " + myEntity);
}
@PreRemove
void onPreRemove(MyEntity myEntity) {
System.out.println("MyEntityListener.onPreRemove(): " + myEntity);
}
@PostRemove
void onPostRemove(MyEntity myEntity) {
System.out.println("MyEntityListener.onPostRemove(): " + myEntity);
}
}
The above annotation will take responsible for the notification .
If any operation will be there in the mentioned table in your application then we will get notification
.
Note:We can use other annotation in Entity class as well.