Last active
August 30, 2015 06:02
-
-
Save dungdm93/f0e17b19e3cba52a61c0 to your computer and use it in GitHub Desktop.
[Java][JPA] Listener & Callback example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[EL Info]: connection: 2015-08-30 12:52:11.554--ServerSession(1286783232)--/file:/C:/Users/KiT/IdeaProjects/Database Sample/service/target/classes/_pu login successful | |
Employee.prePersist | |
[EL Fine]: sql: 2015-08-30 12:52:11.635--ClientSession(1437983537)--Connection(1614079837)--INSERT INTO EMPLOYEE (NAME, SALARY) VALUES (?, ?) | |
bind => [2 parameters bound] | |
[EL Fine]: sql: 2015-08-30 12:52:11.637--ClientSession(1437983537)--Connection(1614079837)--SELECT LAST_INSERT_ID() | |
Employee.postPersist | |
======================================== | |
[EL Fine]: sql: 2015-08-30 12:52:11.729--ServerSession(1286783232)--Connection(1614079837)--SELECT ID, NAME, SALARY FROM EMPLOYEE WHERE (ID = ?) | |
bind => [1 parameter bound] | |
Employee.postLoad | |
[EL Info]: connection: 2015-08-30 12:52:11.734--ServerSession(1286783232)--/file:/C:/Users/KiT/IdeaProjects/Database Sample/service/target/classes/_pu logout successful |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.persistence.*; | |
public class LogListener { | |
@PrePersist | |
public void prePersist(Object object) { // MUST have a parameter | |
System.out.println("Employee.prePersist"); | |
} | |
@PostPersist | |
public void postPersist(Object object) { // MUST have a parameter | |
System.out.println("Employee.postPersist"); | |
} | |
@PostLoad | |
public void postLoad(Object object) { // MUST have a parameter | |
System.out.println("Employee.postLoad"); | |
} | |
... // more callback functions | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.persistence.*; | |
public class ServiceJPA { | |
public static void create(EntityManager manager) { | |
EntityTransaction transaction = manager.getTransaction(); | |
transaction.begin(); | |
Employee employee = new Employee("KiT", 123456789); | |
manager.persist(employee); | |
transaction.commit(); | |
} | |
public static void load(EntityManager manager, int id) { | |
EntityTransaction transaction = manager.getTransaction(); | |
transaction.begin(); | |
Employee employee = manager.find(Employee.class, id); | |
transaction.commit(); | |
} | |
public static void main(String[] args) { | |
EntityManagerFactory factory = Persistence.createEntityManagerFactory("pu"); | |
EntityManager manager = factory.createEntityManager(); | |
create(manager); | |
System.out.println("========================================"); | |
load(manager, 1); | |
manager.close(); | |
factory.close(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.persistence.*; | |
@Entity | |
@EntityListeners(LogListener.class) | |
public class Employee { | |
@Id | |
@GeneratedValue(strategy = GenerationType.IDENTITY) | |
private int id; | |
private String name; | |
private long salary; | |
@Override | |
public String toString() { | |
return String.format("Employee #%d: %s (%d)", id, name, salary); | |
} | |
} |
Author
dungdm93
commented
Aug 30, 2015
- Callback functions
- Listeners
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment