Java Tutorial/JPA/EntityListener — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 05:18, 1 июня 2010
Содержание
Add Entity Listeners To subclasses
File: EmployeeDebugListener.java
import javax.persistence.PostLoad;
import javax.persistence.PrePersist;
import javax.persistence.PreRemove;
import javax.persistence.PreUpdate;
public class EmployeeDebugListener {
@PostLoad
public void postLoad(Employee emp) {
System.out.println("EmployeeDebugListener.Load called on employee id: " + emp.getId());
}
}
Entity Listener: PostLoad
File: EmployeeDebugListener.java
import javax.persistence.PostLoad;
import javax.persistence.PrePersist;
import javax.persistence.PreRemove;
import javax.persistence.PreUpdate;
public class EmployeeDebugListener {
@PostLoad
public void postLoad(Employee emp) {
System.out.println("EmployeeDebugListener.Load called on employee id: " + emp.getId());
}
}
Entity Listener: PreRemove
File: Employee.java
import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.Id;
import javax.persistence.PostLoad;
import javax.persistence.PostPersist;
import javax.persistence.PostUpdate;
import javax.persistence.PrePersist;
import javax.persistence.PreRemove;
import javax.persistence.PreUpdate;
import javax.persistence.Transient;
@Entity
@EntityListeners({EmployeeDebugListener.class})
public class Employee {
@Id private int id;
private String name;
@Transient private long syncTime;
@PostPersist
@PostUpdate
@PostLoad
private void resetSyncTime() {
syncTime = System.currentTimeMillis();
System.out.println("Employee.resetSyncTime called on employee id: " + getId());
}
public long getCachedAge() {
return System.currentTimeMillis() - syncTime;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String toString() {
return "Employee id: " + getId() ;
}
}
Entity Listener: PreUpdate
File: EmployeeDebugListener.java
import javax.persistence.PostLoad;
import javax.persistence.PrePersist;
import javax.persistence.PreRemove;
import javax.persistence.PreUpdate;
public class EmployeeDebugListener {
@PreUpdate
public void preUpdate(Employee emp) {
System.out.println("EmployeeDebugListener.Update called on employee id: " + emp.getId());
}
}
Entity Listeners PrePersist
File: Employee.java
import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.Id;
import javax.persistence.PostLoad;
import javax.persistence.PostPersist;
import javax.persistence.PostUpdate;
import javax.persistence.PrePersist;
import javax.persistence.PreRemove;
import javax.persistence.PreUpdate;
import javax.persistence.Transient;
@Entity
@EntityListeners({EmployeeDebugListener.class, NameValidator.class})
public class Employee {
@Id private int id;
private String name;
@Transient private long syncTime;
@PostPersist
@PostUpdate
@PostLoad
private void resetSyncTime() {
syncTime = System.currentTimeMillis();
System.out.println("Employee.resetSyncTime called on employee id: " + getId());
}
public long getCachedAge() {
return System.currentTimeMillis() - syncTime;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String toString() {
return "Employee id: " + getId() ;
}
}
Exclude Super class Listeners In Super Class
File: EmployeeDebugListener.java
import javax.persistence.PostLoad;
import javax.persistence.PrePersist;
import javax.persistence.PreRemove;
import javax.persistence.PreUpdate;
public class EmployeeDebugListener {
@PostLoad
public void postLoad(Employee emp) {
System.out.println("EmployeeDebugListener.Load called on employee id: " + emp.getId());
}
}
One Entity With Two Listeners
File: EmployeeDebugListener.java
import javax.persistence.PostLoad;
import javax.persistence.PrePersist;
import javax.persistence.PreRemove;
import javax.persistence.PreUpdate;
public class EmployeeDebugListener {
@PrePersist
public void prePersist(Employee emp) {
System.out.println("EmployeeDebugListener.Persist called on employee id: " + emp.getId());
}
}