Monday, 20 August 2012

JPA Lifecycle Annotations

This post illustrates how to use JPA's life cycle annotations. These can be used on method to perform actions before or after CRUD operations. The code example is available from Github in the JPA directory.

We define two classes, one with annotations WithLifeCycleAnnotations and one without annotations NoLifeCycleAnnotations. This example operates Hibernate JPA in a standalone in-memory mode.

With annotations:
@Entity
public class WithLifeCycleAnnotations {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private long ID;

    private String name = "";

    @PostLoad
    public void postLoad() {
        System.out.println("Post Load called !!!");
    }

    @PostPersist
    public void postPersist() {
        System.out.println("Post Persist called !!!");
    }

    @PostRemove
    public void postRemove() {
        System.out.println("Post Remove called !!!");
    }

    @PostUpdate
    public void postUpdate() {
        System.out.println("Post Update called !!!");
    }

    @PrePersist
    public void prePersist() {
        System.out.println("Pre Persist called !!!");
    }

    @PreRemove
    public void preRemove() {
        System.out.println("Pre Remove called !!!");
    }

    @PreUpdate
    public void preUpdate() {
        System.out.println("Pre Update called !!!");
    }

    // Setters & Getters...

}
The following:
NoLifeCycleAnnotations nlca = new NoLifeCycleAnnotations();
nlca.setName("AAA");

System.out.println("Saving NO lifecycle annotations");
JPA.INSTANCE.save(nlca);

System.out.println("Reading NO lifecycle annotations");
NoLifeCycleAnnotations nlcaRetr =
    JPA.INSTANCE.get(NoLifeCycleAnnotations.class, nlca.getID());

System.out.println("Updating NO lifecycle annotations");
nlca.setName("BBB");
JPA.INSTANCE.update(nlca);

System.out.println("Deleting NO lifecycle annotations");
nlca.setName("BBB");
JPA.INSTANCE.delete(nlca);


WithLifeCycleAnnotations wlca = new WithLifeCycleAnnotations();
wlca.setName("AAA");

System.out.println("Saving WITH lifecycle annotations:");
JPA.INSTANCE.save(wlca);

System.out.println("Reading WITH lifecycle annotations:");
WithLifeCycleAnnotations wlcaRetr =
    JPA.INSTANCE.get(WithLifeCycleAnnotations.class, wlca.getID());

System.out.println("Updating WITH lifecycle annotations:");
wlca.setName("BBB");
JPA.INSTANCE.update(wlca);

System.out.println("Deleting WITH lifecycle annotations:");
wlca.setName("BBB");
JPA.INSTANCE.delete(wlca);
Generates the following output:
Saving NO lifecycle annotations
Reading NO lifecycle annotations
Updating NO lifecycle annotations
Deleting NO lifecycle annotations

Saving WITH lifecycle annotations:
Pre Persist called !!!
Post Persist called !!!
Reading WITH lifecycle annotations:
Updating WITH lifecycle annotations:
Pre Update called !!!
Post Update called !!!
Deleting WITH lifecycle annotations:
Pre Remove called !!!
Post Remove called !!
For some reason, @PostLoad is not invoked when using JPA/Hibernate in standalone mode.