Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JPA @PostPersist usage

I have a method persistData() which persists an entity object. I have another method findData() which performs find() operation on the same entity class for the primary key value which was persisted. When I call the findData() in the @PostPersist of the entity class, I get a null pointer exception. This has raised a few questions in my mind:

  1. Why is it giving a null pointer error?
  2. What is the use of @PostPersist in reality?
  3. When is a @Postpersist actually called? After commit, during commit or before commit?

Any further insights would also be appreciated. Please find the relevant code and stacktrace below:

public void persistData(){
        EntityManagerFactory fac= Persistence.createEntityManagerFactory("test");
        EntityManager man = fac.createEntityManager();

        Employee e = new Employee();
        e.setEmpId(500);
        e.setEmpName("Emp5");
        e.setSalary(5000);
        man.getTransaction().begin();
        man.persist(e);
        man.getTransaction().commit();
        man.close();

    }



public void findData(){
        EntityManagerFactory fac= Persistence.createEntityManagerFactory("test");
        EntityManager man = fac.createEntityManager();

        Employee e=man.find(Employee.class, 500);
        System.out.println(e.getEmpName());
        man.close();    
    }

@PostPersist
public void getData(){
    new Service().findData();
}

Stack Trace ( Partial ):

Exception in thread "main" javax.persistence.RollbackException: java.lang.NullPointerException
    at oracle.toplink.essentials.internal.ejb.cmp3.transaction.base.EntityTransactionImpl.commit(EntityTransactionImpl.java:120)
    at oracle.toplink.essentials.internal.ejb.cmp3.transaction.EntityTransactionImpl.commit(EntityTransactionImpl.java:60)
    at Service.persistData(Service.java:18)
    at Service.main(Service.java:34)
Caused by: java.lang.NullPointerException
    at Service.findData(Service.java:28)
    at Employee.getData(Employee.java:33)

Note: I am using JPA 1.0

like image 965
codingsplash Avatar asked Mar 29 '13 11:03

codingsplash


People also ask

Is @column mandatory in JPA?

Let's start with the @Column annotation. It is an optional annotation that enables you to customize the mapping between the entity attribute and the database column.

What is JPA and why it is used?

JPA stands for Java Persistence API (Application Programming Interface). It was initially released on 11 May 2006. It is a Java specification that gives some functionality and standard to ORM tools. It is used to examine, control, and persist data between Java objects and relational databases.

What is @PrePersist in JPA?

JPA specifies seven optional lifecycle events that are called: before persist is called for a new entity – @PrePersist. after persist is called for a new entity – @PostPersist. before an entity is removed – @PreRemove. after an entity has been deleted – @PostRemove.

What is @PrePersist and @PostPersist?

@PrePersist: The method annotated with @PrePersist in listener bean class is called before persisting data by entity manager persist() method. @PostPersist: The method annotated with @PostPersist is called after persisting data.


1 Answers

To answer your question 1:

(need the code and the stacktrace)

To answer your question 2:

The @PostPersist indicate a JPA callback method. It allows you to trigger some code through the entity life-cycle events.

A real life example ?

Assume you have a User table and you want to generate a confirmation email every time a new User is persisted: you can do it in a PostPersist method.

To answer your question 3:

The relevant part of specs are in blod.

From JPA-2.0 specs:

The PostPersist and PostRemove callback methods are invoked for an entity after the entity has been made persistent or removed. These callbacks will also be invoked on all entities to which these operations are cascaded. The PostPersist and PostRemove methods will be invoked after the database insert and delete operations respectively. These database operations may occur directly after the persist, merge, or remove operations have been invoked or they may occur directly after a flush operation has occurred (which may be at the end of the transaction). Generated primary key values are available in the PostPersist method.

like image 95
ben75 Avatar answered Oct 29 '22 18:10

ben75