-->
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.  [ 5 posts ] 
Author Message
 Post subject: pre-updateEventListener and DB changes to entities
PostPosted: Tue Jan 24, 2006 1:21 pm 
Newbie

Joined: Mon Jan 23, 2006 11:58 am
Posts: 1
Considering an Entity with an historize property. (The idea is the property to be a string that records changes on old and new states of the same entity's properties).

Before updating the DB, i want that historize propertyvalue to be set to a new String value returned by a method that CheckChanges between the old and actualState of the entity's properties. The Entity being intercepted via the generated pre-updateEvent by my custom listener (which implements DefaultPre-updateEventListener).

on flush the generated event is well intercept by my custompreUpdateEventListener class.

Hence I can change my event.getEntity historize property to the new String value tracking the changes that have occured in the open session....

BUT I am unable to reproduce this change in the DataBase.

Here follows some of the code I used.....

Does anyone have suggestions on how to validate the entity changes in the DB??

Or maybe suggest some alternatives for the same "history property "functionality.

Many thanks for your Suggestions.



Hibernate version:3.0

Mapping documents:

Hibernate mapping file referencing my EventListener
<listener type="pre-update" class="fr.odima.portail.is.UpdateAnomalieEventListener"/>

Code between sessionFactory.openSession() and session.close():

......
elsewhere in the code session.flush wich generates the preUpdateEvent
.....

public class UpdateAnomalieEventListener extends DefaultPreUpdateEventListener {

public boolean onPreUpdate(PreUpdateEvent event) {


Object toUpdate =event.getEntity();
Object[] oldState = event.getOldState();
Object[] newState= event.getState();

if (condition to check if entity istanceof Anomalie){
/*....code that checks difference between old and new state and record the difference in the
String newHisto =oldHistorique +histo.toString();*/

((Anomalie)event.getEntity()).setHistorique(newHisto);
return true;}

else {return super.onPreUpdate(event);}
}


......
elsewhere in the code transaction.commit
.....


Full stack trace of any exception that occurs:
No exception. Changes are valid on entity but not commited in DB.

Name and version of the database you are using:
PostgreSQL 1.2.2

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 07, 2006 3:44 am 
Regular
Regular

Joined: Tue May 03, 2005 8:19 am
Posts: 53
Location: Paris
I have the same problem with version 3.0.5 and 3.1.2

I thought I could use a PreXXXEventListener to update my objects before an action on the database, as would have done a Trigger before.

My Listener is effectively well called, but the information added to my object by this method is not saved in the database.

Here is the test illustrating this issue with a PreInsertEventListener. I have the same problem with the PreUpdateEventListener.

I hope I made myself clear.

I have a junit test which illustrates the problem, this code can be seen in my french post

Thanks Thierry for translation


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 12, 2006 2:15 pm 
Newbie

Joined: Mon Mar 13, 2006 7:07 pm
Posts: 8
I am having the same problem.

I am able to edit a new entity being saved using a PostInsertEventListener.
But using similar code in a PostUpdateEventListener does nothing.


Can anyone help?

Here is what I am trying to do:

Code:
public class ClientSaveEventListener implements PostInsertEventListener, PostUpdateEventListener
{

   public ClientSaveEventListener()
   {
      super();
      // TODO Auto-generated constructor stub
   }

   public void onPostInsert(PostInsertEvent event)
   {   
      if(event.getEntity() instanceof Client)
      {
         Client client = (Client) event.getEntity();
         Note note = new Note();
         note.setNote("Client Added.");
         note.setDate(new Date());
         client.getNotes().add(note);
      }
   }

   public void onPostUpdate(PostUpdateEvent event)
   {
      if(event.getEntity() instanceof Client)
      {   
         Client client = (Client) event.getEntity();
         Note note = new Note();
         note.setNote("Client Updated.");
         note.setDate(new Date());
         client.getNotes().add(note);
      }
   }
}



Again, both events actually fire when they are supposed to. And onPostInsert works as expected. onPostUpdate does not propagate its changes to the database.

Any help would be greatly appreciated.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 05, 2006 3:59 am 
Newbie

Joined: Wed Jul 05, 2006 3:29 am
Posts: 1
Location: Germany
I had the same problems with the Hibernate eventsystem. After some time debugging and testwritings noticed I that the PreUpdate/PreInsert event is only fired, when you use transactions and the entity which is going to be saved has been changed.

In a small testapplication I have tried out three cases which are the
following:

1) save / update without transaction and changes to entity
2) save / update with transactions without changed to entity
3) save / update with transactions and changes to entity

In my case only the third constellation causes the preupdate / preinsert events.


Here's an extract of my testcode:

Code:

public class SelectForUpdateTest {
    // encases an object of org.hibernate.Session
    // with some additional logic
    public static HibernateConnectionBean dbconnect = null;
   
    public static void main(String[] args) throws Exception {
        dbconnect = new HibernateConnectionBean();
        dbconnect.setHibernate_CFG_File("/test/hibernate.cfg.xml");
        dbconnect.setUsername("me");
        dbconnect.setPassword("alsome");
        dbconnect.openDatabaseConnection();

        // is the connection established ?
        SelectForUpdateTest.CheckDatabaseConnection();
       
        SelectForUpdateTest.testManuallyCommit();
       
        SelectForUpdateTest.testWithoutChange();
       
        SelectForUpdateTest.testWithoutCommit();
       
        dbconnect.closeDatabaseConnection();
       
    }
   
    public static void testManuallyCommit() {
        // get a bean
        MyBean bw = SelectForUpdateTest.getSomeBean();
        System.out.println(bw.getName());
       
        Transaction tx = null;
       
        tx = dbconnect.getSession().beginTransaction();
        bw.setName(Long.toString(System.currentTimeMillis()));
        dbconnect.getSession().saveOrUpdate(bw);
        tx.commit();
    }
   
    public static void testWithoutCommit() {
        // get a bean
        MyBean bw = SelectForUpdateTest.getSomeBean();
        System.out.println(bw.getName());
       
        bw.setName(Long.toString(System.currentTimeMillis()));
        dbconnect.getSession().saveOrUpdate(bw);
    }
   
   
    public static void testWithoutChange() {
        // get a bean
        MyBean bw = SelectForUpdateTest.getSomeBean();
        System.out.println(bw.getName());
       
        Transaction tx = null;
       
        tx = dbconnect.getSession().beginTransaction();
        dbconnect.getSession().saveOrUpdate(bw);
        tx.commit();
    }   

    public static MyBean getSomeBean() {
   // ...........
    }

}


Hope that helps.

Benjamin


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 05, 2006 5:27 am 
Regular
Regular

Joined: Tue May 03, 2005 8:19 am
Posts: 53
Location: Paris
All modifications on event.getEntity() in the PostInsertEventListener have no effect because Hibernate used event.getState() in the query SQL.

See my post French :
http://forum.hibernate.org/viewtopic.php?t=954867


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