-->
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.  [ 2 posts ] 
Author Message
 Post subject: Use of session in onSave
PostPosted: Mon Jan 26, 2004 8:13 am 
Newbie

Joined: Mon Sep 22, 2003 5:43 am
Posts: 15
Succinct Problem:
When I use an s.find("..") in onSave (Lifecycle) I get the following error: "SQL update or deletion failed (row not found)"

Using: hib2.1 with spring managed transactions through intercptors.

Is there a limitation to the use of find or the session whilst inside the onSave callback?

Verbose Problem:

I want to export a complex object graph to XML. And then import updates from XML.

I use a surrogate key (an int) in the database to identify unique objects, but I also use a uniqueId to identify my objects when in XML form.

eg:
Agent id:007 uid:Bond
in xml only the uid is exported:
<agent uid="Bond">xxxxxxx</agent>

Everything works fine but if I import an xml file wich includes "bond" when I save the agents to the db the unsaved-value causes all the agents to be created as new.

You may well ask why I don't update objects one-by-one. Well, I have a large object graph and cascade is working well up to now, I'd prefer to intercept the saves and then implement my own update.

So, I implemented this in onSave



Code:
public boolean onSave(Session s) throws CallbackException {
   logger.debug("onSave... Agent. id="+getId() +" uid=" + getName());

   //Check to see if uid is in the database
   try {
      logger.debug("Checking UID");
      List agents = s.find("from com.xxx.Agent a where a.nam=?",
            this.getName(), Hibernate.STRING);
      return false;
      //now check for Agents with this key
      if (Agents.size()>0) {
         logger.debug("uid match: "+this.getName()+" Swap objects, evict and update.");
         Agent agent = (Agent) Agents.get(0);
         //no we will swap this object for the Agent in
         //memory and update (maintaining the id value)

         //set id equal
         this.setId(agent.getId());
         s.evict(agent);
         s.update(this);

         //veto save
         logger.debug("Vetoing save");
         return true;

      } else {
         //no uid exists save continues as normal
         logger.debug("No matching Uid: " + this.getName());
         return super.onSave(s);
      }
   } catch (HibernateException e) { //
      //throw on error
      logger.debug(e);
      throw new CallbackException(e);
   }

}


But, I get terrible problems with "SQL update or deletion failed (row not found)" errors. I presume this is because of somehow accessing / changing data that is not yet written to the db. But as all of the object creation and modification is within the same session I thought this would all be handled by Hibernate. Any comments?

Thanks, Dan.


Top
 Profile  
 
 Post subject: Own Answer
PostPosted: Mon Jan 26, 2004 10:27 am 
Newbie

Joined: Mon Sep 22, 2003 5:43 am
Posts: 15
Quote:
The normal thought process would be to perform the validations during the onSave(), onFlushDirty(), etc callbacks. But the validations cannot be setup that way because it is illegal to interact with the session in any way (and yes this includes triggering lazy initialization) during these callbacks. Doing so causes very weird side-effects.


So in answer to my question what I asked above can't be done.
The quote abouve came from the thread below, it looks like the way to go.

http://forum.hibernate.org/viewtopic.php?t=925103&highlight=onsave

[/quote]


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