Howdy!
We're using JPA with Hibernate as JPA provider with so far great success. Then we needed to use the @PrePersist, @PostLoad and @PreUpdate annotations. PostLoad works great, no complaints, but the @PrePersist and @PreUpdate annotated methods aren't called when they should and I have no idea why.
To make things simpler we wrote a test entity class and the problem persists (:-)).
Using this class we can load (and the PostLoad method is called) but not PreUpdate and PrePersist.
Our entity class:
Code:
package com.memnon.apport.model;
import com.memnon.apport.model.AbstractApportEntity;
import com.memnon.apport.db.PersistedIn;
import javax.persistence.*;
import java.io.Serializable;
@Entity
public class JpaTestEntity extends AbstractApportEntity implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int pk;
private String firstName;
private String lastName;
private String address;
@Transient
private String name;
@PostLoad
private void post(){
System.out.println("post()");
if(firstName != null ){
name = firstName + " ";
}
if(lastName != null){
name += lastName;
}
}
@PrePersist
@PreUpdate
private void pre(){
System.out.println("pre()");
if(name != null){
String[] names = name.split(" ");
firstName = names[0];
lastName = names[1];
}
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPk() {
return pk;
}
public void setPk(int pk) {
this.pk = pk;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "JpaTestEntity{" +
"pk=" + pk +
", firstName='" + firstName + '\'' +
", lastName='" + lastName + '\'' +
", adress='" + address + '\'' +
", name='" + name + '\'' +
'}';
}
}