-->
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.  [ 9 posts ] 
Author Message
 Post subject: Problem with PreINsertEventListener
PostPosted: Wed Aug 30, 2006 12:15 pm 
Beginner
Beginner

Joined: Sat Oct 08, 2005 2:13 am
Posts: 47
Hello

Hibernate version:3.1

Mapping documents:Annotation

Code between sessionFactory.openSession() and session.close():
User u = new User();
u.setUsername("test_user");
u.setPassword("test_pass");

userDao.save(u);


Full stack trace of any exception that occurs:No Exception

Name and version of the database you are using:MySql 4

I create a pre insert event listener to set a date/time field and the user name who is going to insert this record. but it doesnt work

Code:
public boolean onPreInsert(PreInsertEvent e){
     if(e.getEntity() instanceof MyBaseEntity){
          MyBaseEntity be = (MyBaseEntity) e.getEntity();
          be.setCreateDate(new Date());
          be.setCreatorUser("a_username");

     }
     return false;
}


the event has been fired but no changes applied.

would you help me out?

regards
Mohammad


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 18, 2008 1:21 pm 
Beginner
Beginner

Joined: Tue Nov 06, 2007 5:13 am
Posts: 28
did U figure out how to do this?

seems like no-one is interested in pre-insert/pre-update issues
I have found a lot of questions in forums and in the web abouth this, but no answers...


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 19, 2008 9:04 am 
Beginner
Beginner

Joined: Sat Oct 08, 2005 2:13 am
Posts: 47
Hi
actually I can't remember what I did at that time. perhaps I left it but as far as I know if you are using Hibernate 3.1 you can use @PrePersist and @PreUpdate annotations that they come from EJB 3 and I think it will work fine.

_________________
Regards,
photos: http://pixelshot.wordpress.com
weblog: http://brainable.blogspot.com


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 19, 2008 9:08 am 
Beginner
Beginner

Joined: Sat Oct 08, 2005 2:13 am
Posts: 47
as an example:

Code:
class User {

   String username;
   String password;
   Date updateTime;
   //other fields....

   //getters and setters methods....

   @PreUpdate
   public void dateOfUpdate(){
      updateTime = new Date();
   }

}

_________________
Regards,
photos: http://pixelshot.wordpress.com
weblog: http://brainable.blogspot.com


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 19, 2008 9:43 am 
Beginner
Beginner

Joined: Tue Nov 06, 2007 5:13 am
Posts: 28
tx for the answer

I know about those annotation, but that would mean, that I have to put them on every entity that wants to write a history
or maybe it would work if I put it on a common superclass of all entities that want to keep a history

what my prefered method is:
  • the entities that are interested in historical data must implement a special interface (e.g. Historical)
  • there is a pre-insert and pre-update event listener, that will be called for all entities: inside this lisitener, I only need to check if the entitiy is an instance of my Historical inteface

I already got that working, but still have a problem, that is a bit like your's:
when I create my entity it's like that:
  • pre-insert event listener is called
  • Historical data is set on the entity (I checked this in the debugger)
  • hibernate issues a sql-insert command: but without the data that has been set in teh pre-insert event listener!
  • hibernate now issues an update command and sets the historical data that has been set in the pre-insert event listener

that sounds a lot like what you have encountered in your original post

I have already tried to find more information on this, but didn't find anything that would help me
the API doc of the PreInsertEventListener does not even mention what the expected returnvalue means!
http://www.hibernate.org/hib_docs/v3/api/org/hibernate/event/PreInsertEventListener.html


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 03, 2008 8:04 am 
Newbie

Joined: Tue Sep 05, 2006 10:39 am
Posts: 13
I have a prePersist listener set up on the superclass of all my entities but it is ignored. It looks very similar to your situation.

I have a 'modified' field on the superclass which the prePersist callback should set to the current datetime.

This is using hibernate-entitymanager-3.3.1ga and hibernate core 3.2.5.

There's no related issue in Jira that I can see. If nobody says otherwise, I'll open one for it.

regards
Adam

_________________
Please rate my post, thanks.

Those are my principles. If you don't like them I have others. -- Groucho Marx


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 03, 2008 8:51 am 
Beginner
Beginner

Joined: Tue Nov 06, 2007 5:13 am
Posts: 28
+1 from me for the jira.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 06, 2008 7:59 pm 
Newbie

Joined: Tue Sep 05, 2006 10:39 am
Posts: 13
http://opensource.atlassian.com/project ... se/EJB-342

_________________
Please rate my post, thanks.

Those are my principles. If you don't like them I have others. -- Groucho Marx


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 25, 2008 10:28 am 
Beginner
Beginner

Joined: Tue Nov 06, 2007 5:13 am
Posts: 28
mars1412 wrote:
tx for the answer

I already got that working, but still have a problem, that is a bit like your's:
when I create my entity it's like that:
  • pre-insert event listener is called
  • Historical data is set on the entity (I checked this in the debugger)
  • hibernate issues a sql-insert command: but without the data that has been set in teh pre-insert event listener!
  • hibernate now issues an update command and sets the historical data that has been set in the pre-insert event listener
that sounds a lot like what you have encountered in your original post



I got that working now.
I extend org.Hibernate.EmptyInterceptor and override onSave and onFlushDirty.
Both cases work as expected, now.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 9 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.