-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: JPA PrePersist, PreUpdate not called
PostPosted: Thu Feb 04, 2010 9:52 am 
Newbie

Joined: Tue Aug 09, 2005 10:29 am
Posts: 10
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 + '\'' +
               '}';
   }
}


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.