-->
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.  [ 4 posts ] 
Author Message
 Post subject: PrePersist/PostPersist callback method are NOT called
PostPosted: Sat Jul 17, 2010 11:27 am 
Newbie

Joined: Sat Jul 17, 2010 10:33 am
Posts: 4
I am struggling with some simple usage of @PrePost and @PostPersist, the entity is persisted and rows are inserted into my DB, and there are no exceptions, but the methods tagged with PrePersist/PostPersist are simply not called - everything before or after is gently outputted except the message in the PostPersist/PrePersist method. Here is the test method I use:
Code:
@PostPersist
public void onUserPersisted() {
  System.out.println("Post-persist");
  throw new Error(); //Just to ensure that if output is redirected, I get an error
}


Then I tried another example from java2s, here is the link with the source code:
http://www.java2s.com/Tutorial/Java/035 ... ersist.htm

I slightly changed the above example by replacing EntityManagerFactory with SessionFactory and Session, so the modification looks like this:
Code:
package com.cbfa.entity.listeners;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;

public class ListenerJava2sExample {
   
     static SessionFactory sf = new AnnotationConfiguration()
     .addAnnotatedClass(Employee.class)  // !! (strange thing #2)
          .configure().buildSessionFactory();
    
     static Session session = sf.openSession();

     public static void main(String[] a) throws Exception {
       session.getTransaction().begin();
      
       Employee emp = new Employee();
      
       emp.setId(1);
      
       session.persist(emp);

       session.getTransaction().commit();
       session.close();
       sf.close();
     }
   }

In addition, I omitted the Helper class, as I already have a working configuration of my own.

I am using JavaSE library, and have added the following to the classpath:
    mysql-connector-java-5.1.13-bin.jar
    junit-4.4.jar
    hibernate-core-3.3.2.GA.jar
    dom4j-1.6.1.jar
    slf4j-log4j12-1.5.8.jar
    slf4j-api-1.5.8.jar
    log4j-1.2.14.jar
    commons-collections-3.2.1.jar
    jta-1.1.jar
    hibernate-annotations-3.4.0.GA.jar
    hibernate-validator-3.1.0.GA.jar
    hibernate-commons-annotations-3.3.0.ga.jar
    javassist.jar
    ejb3-persistence-1.0.2.GA.jar

Hibernate settings are lets say, copied from a tutorial, nothing special.

Another strange thing, which may be related to this problem is that if I don't add annotated class manually to session factory (see line with comment "!! (strange thing #2)"), I get the "Unkown entity" exception. In the tutorials I have seen so far, there was no need to call "addAnnotatedClass" to ensure that Entity tag is recognized.
I don't know if this may help, but I also noticed that when I tag entities with @org.hibernate.annotations.Entity
(instead of @javax.persistence.Entity), persistence for that entity does not work at all.

Am I doing something really wrong or ... ? Do I have to explicitly enable the usage of callback methods?


Top
 Profile  
 
 Post subject: Re: PrePersist/PostPersist callback method are NOT called
PostPosted: Wed Jul 28, 2010 3:18 pm 
Newbie

Joined: Sun Jun 27, 2010 5:05 pm
Posts: 3
I'm having the same problem. not sure if there's some other setup required.


Top
 Profile  
 
 Post subject: Re: PrePersist/PostPersist callback method are NOT called
PostPosted: Wed Oct 06, 2010 10:53 am 
Newbie

Joined: Wed Oct 06, 2010 10:49 am
Posts: 1
Hi, having the same trouble.
Did you get it working ?


Top
 Profile  
 
 Post subject: Re: PrePersist/PostPersist callback method are NOT called
PostPosted: Tue Mar 29, 2011 4:21 pm 
Newbie

Joined: Tue Mar 29, 2011 11:43 am
Posts: 3
Hi,

Is there anybody's got a solution to that issue ?

I have the same one and it doesn't seem to have answers !

I worked on a other project using hibernate as JPA provider and it used to works without particular tuning.

Today I'm on an hibernate annotation (hibernate 3.6.2) project, but the @PrePersist methods are never called !

Is there some particular params to add to hibernate config or is there an other way to have some life cycle event in hibernate annotation ?

Regards


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

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.