SlideShare a Scribd company logo
JPA Lifecycle
Events Practice

Albert
junyuo@gmail.com
Lifecycle Callbacks
 It
   is often necessary to perform various
  actions at different stages of a persistent
  object's lifecycle. JPA includes a variety of
  callbacks methods for monitoring
  changes in the lifecycle of your persistent
  objects. These callbacks can be defined
  on the persistent classes themselves and
  on non-persistent listener classes.
Callback Methods
Callback method   Description


@PrePersist       before a new entity is persisted (added to the
                  EntityManager).
@PostPersist      after storing a new entity in the database (during commit
                  or flush).
@PostLoad         after an entity has been retrieved from the database.
@PreUpdate        when an entity is identified as modified by the
                  EntityManager.
@PostUpdate       after updating an entity in the database (during commit
                  or flush).
@PreRemove        when an entity is marked for removal in the
                  EntityManager.
@PostRemove       after deleting an entity from the database (during
                  commit or flush).
Using Callback Methods
Using Entity Listeners
The listener class is attached to the entity class using the
@EntityListeners annotation




Multiple listener classes can also be attached to one entity class
Example
NIG005W




User uses this function to maintain organization information.
Schema




We intend to utilize JPA callback method to write user id and update
timestamp into database automatically to ease programmers’ efforts.
Using Callback Methods
          Multiple events can be assigned to
          a single method as well. This method
          Will be triggered before insert and update.
Using Entity Listeners
              Mixing lifecycle event code into your
              persistent classes is not always ideal.
              It is often more elegant to handle
              cross-cutting lifecycle events in a
              non-persistent listener class.
              JPA allows for this, requiring only that
              listener classes have a public no-arg
              constructor. Like persistent classes,
              your listener classes can consume any
               number of callbacks. The callback
              methods must take in a single
              java.lang.Object argument which
              represents the persistent object that
              triggered the event.

More Related Content

PPTX
OCEJPA(1Z0-898) Preparation Tips
PPTX
Introduction to JPA Framework
PPTX
JPA For Beginner's
PPT
PPTX
Introduction to jQuery
DOCX
Java interview questions and answers for cognizant By Data Council Pune
PPT
Java Persistence API (JPA) Step By Step
PPT
Java persistence api
OCEJPA(1Z0-898) Preparation Tips
Introduction to JPA Framework
JPA For Beginner's
Introduction to jQuery
Java interview questions and answers for cognizant By Data Council Pune
Java Persistence API (JPA) Step By Step
Java persistence api

What's hot (20)

PDF
Java Interview Questions
ODP
Hibernate complete Training
PPT
Entity Persistence with JPA
PPTX
Introduction to JPA (JPA version 2.0)
PPTX
Jpa 2.1 Application Development
ODP
JPA Best Practices
PPT
jpa-hibernate-presentation
PDF
Class method object
PDF
Java persistence api 2.1
PPT
Java API, Exceptions and IO
PDF
Object oriented thinking
PPT
Hibernate introduction
PPTX
JavaOne 2014 - CON2013 - Code Generation in the Java Compiler: Annotation Pro...
PDF
Introduction to JPA and Hibernate including examples
PPTX
Java Annotations
PDF
Advanced java interview questions
PPTX
Lecture - 1 introduction to java
PPTX
Spring & hibernate
PPTX
Java Class 6 | Java Class 6 |Threads in Java| Applets | Swing GUI | JDBC | Ac...
PPT
Introduction to Hibernate
Java Interview Questions
Hibernate complete Training
Entity Persistence with JPA
Introduction to JPA (JPA version 2.0)
Jpa 2.1 Application Development
JPA Best Practices
jpa-hibernate-presentation
Class method object
Java persistence api 2.1
Java API, Exceptions and IO
Object oriented thinking
Hibernate introduction
JavaOne 2014 - CON2013 - Code Generation in the Java Compiler: Annotation Pro...
Introduction to JPA and Hibernate including examples
Java Annotations
Advanced java interview questions
Lecture - 1 introduction to java
Spring & hibernate
Java Class 6 | Java Class 6 |Threads in Java| Applets | Swing GUI | JDBC | Ac...
Introduction to Hibernate
Ad

Similar to JPA lifecycle events practice (20)

PDF
Owner - Java properties reinvented.
PPTX
Presentation2.ppt java basic core ppt .
PDF
Java Faqs useful for freshers and experienced
PDF
Top 371 java fa qs useful for freshers and experienced
PPT
Md02 - Getting Started part-2
DOCX
Java interview questions
PDF
Java unit 7
PPTX
Chapter 8 java
DOCX
Viva file
PDF
Introduction to Datastore
PDF
Code transformation With Spoon
PPTX
JAVA AND OOPS CONCEPTS.pptx helpful for engineering
DOCX
Java Interview Questions For Freshers
PDF
Asked Java Interview Questions for fresher
PDF
Reactive java - Reactive Programming + RxJava
DOCX
Java notes
DOCX
100 Java questions FOR LOGIC BUILDING SOFTWARE.docx
PDF
Java beans
PPTX
java framwork for HIBERNATE FRAMEWORK.pptx
Owner - Java properties reinvented.
Presentation2.ppt java basic core ppt .
Java Faqs useful for freshers and experienced
Top 371 java fa qs useful for freshers and experienced
Md02 - Getting Started part-2
Java interview questions
Java unit 7
Chapter 8 java
Viva file
Introduction to Datastore
Code transformation With Spoon
JAVA AND OOPS CONCEPTS.pptx helpful for engineering
Java Interview Questions For Freshers
Asked Java Interview Questions for fresher
Reactive java - Reactive Programming + RxJava
Java notes
100 Java questions FOR LOGIC BUILDING SOFTWARE.docx
Java beans
java framwork for HIBERNATE FRAMEWORK.pptx
Ad

More from Guo Albert (20)

PPTX
AWS IAM (Identity and Access Management) Policy Simulator
PPTX
TOEIC 準備心得
PDF
DBM專案環境建置
PDF
JPA Optimistic Locking With @Version
PDF
OCEJPA Study Notes
PPTX
XDate - a modern java-script date library
PDF
How to avoid check style errors
PDF
NIG系統報表開發指南
PDF
Ease Your Effort of Putting Data into History Table
PDF
NIG 系統開發指引
PDF
NIG系統開發文件閱讀步驟
PDF
Form Bean Creation Process for NIG System
PDF
A Short Intorduction to JasperReports
PPTX
Apply Template Method Pattern in Report Implementation
PPTX
Utilize Commons BeansUtils to do copy object
PPTX
Apply my eclipse to do entity class generation
PPTX
Nig project setup quickly tutorial
PPTX
Spring JDBCTemplate
PPTX
Java Server Faces + Spring MVC Framework
PDF
Toms introtospring mvc
AWS IAM (Identity and Access Management) Policy Simulator
TOEIC 準備心得
DBM專案環境建置
JPA Optimistic Locking With @Version
OCEJPA Study Notes
XDate - a modern java-script date library
How to avoid check style errors
NIG系統報表開發指南
Ease Your Effort of Putting Data into History Table
NIG 系統開發指引
NIG系統開發文件閱讀步驟
Form Bean Creation Process for NIG System
A Short Intorduction to JasperReports
Apply Template Method Pattern in Report Implementation
Utilize Commons BeansUtils to do copy object
Apply my eclipse to do entity class generation
Nig project setup quickly tutorial
Spring JDBCTemplate
Java Server Faces + Spring MVC Framework
Toms introtospring mvc

JPA lifecycle events practice

  • 2. Lifecycle Callbacks  It is often necessary to perform various actions at different stages of a persistent object's lifecycle. JPA includes a variety of callbacks methods for monitoring changes in the lifecycle of your persistent objects. These callbacks can be defined on the persistent classes themselves and on non-persistent listener classes.
  • 3. Callback Methods Callback method Description @PrePersist before a new entity is persisted (added to the EntityManager). @PostPersist after storing a new entity in the database (during commit or flush). @PostLoad after an entity has been retrieved from the database. @PreUpdate when an entity is identified as modified by the EntityManager. @PostUpdate after updating an entity in the database (during commit or flush). @PreRemove when an entity is marked for removal in the EntityManager. @PostRemove after deleting an entity from the database (during commit or flush).
  • 5. Using Entity Listeners The listener class is attached to the entity class using the @EntityListeners annotation Multiple listener classes can also be attached to one entity class
  • 7. NIG005W User uses this function to maintain organization information.
  • 8. Schema We intend to utilize JPA callback method to write user id and update timestamp into database automatically to ease programmers’ efforts.
  • 9. Using Callback Methods Multiple events can be assigned to a single method as well. This method Will be triggered before insert and update.
  • 10. Using Entity Listeners Mixing lifecycle event code into your persistent classes is not always ideal. It is often more elegant to handle cross-cutting lifecycle events in a non-persistent listener class. JPA allows for this, requiring only that listener classes have a public no-arg constructor. Like persistent classes, your listener classes can consume any number of callbacks. The callback methods must take in a single java.lang.Object argument which represents the persistent object that triggered the event.