1 Reply Latest reply on Dec 30, 2005 7:47 PM by Emmanuel Bernard

    @PrePersist callback not always called

    Eugene Onischenko Newbie

      Hi, I have two beans

      @Entity(access=AccessType.FIELD)
      public class BeanNameB implements Serializable {
       @Id(generate = GeneratorType.AUTO)
       private int id;
      
       private long creationtime;
       @PrePersist
       public void prePersist() {
       this.creationtime = System.currentTimeMillis();
       System.out.println("BeanNameB.prePersist");
       }
      }
      
      @Entity(access=AccessType.FIELD)
      public class BeanNameA implements Serializable{
       @Id(generate = GeneratorType.AUTO)
       private int id;
      
       private long creationtime;
      
       @OneToOne(cascade = CascadeType.ALL)
       private BeanNameB beanNameB;
      
       public BeanNameA(){};
      
       public BeanNameA(long creationtime){
       this.creationtime=creationtime;
       beanNameB = new BeanNameB();
       }
      
       public void makeNewBeanNameB(){
       beanNameB = new BeanNameB();
       }
      }
      


      The prePersist callback in BeanNameB is called when creating the BeanNameA instance. But there is no prePersist callback when the BeanNameA instance is modified by the makeNewBeanNameB() method.

       BeanNameA bean = new BeanNameA(123);
       manager.persist(bean); //All ok here
      
       bean.makeNewBeanNameB();
       manager.flush(); //The new BeanNameB is created but no prePersist callback
      


      Why there is no prePersist callback?