-->
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.  [ 3 posts ] 
Author Message
 Post subject: Checking an object exists before delete or update
PostPosted: Tue Sep 02, 2003 10:05 am 
Newbie

Joined: Tue Sep 02, 2003 9:47 am
Posts: 8
Hi there, not being able to find anything on this by searching, I wonder if anyone here could help me please.

I am using Hibernate to persist basic domain objects currently and have a couple of questions.

Firstly, is there any way to obtain how many results a session.update(Object) or a session.delete(Object) has affected. If I try and run these on an object already removed from the persistent store I get no error or indication that nothing was there.

Secondly, failing the first question, is there a good way to find out whether the object exists in the persistent store before I call these operations. As I will be using the same session, the documentation says I shouldn't try a session.load(Class, Serializable) to confirm existence, and indeed if I try this I get an exception on the following update saying "Another object was associated with this id (the object with the given id was already loaded)"

example code:

Object result = session.load(target.getClass(), target.getId());
session.update(target);


However the documentation says to use find(), which I am assuming is session.find(String). If I build up a query string manually and then use this however I get exactly the same error.

example:

List result = session.find(query);
if (result.size() > 0)
{
session.update(target);
}

Am I doing something monumentally dumb here and does anyone know what I need to write to do this?

Thanks for any help.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 02, 2003 10:09 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
If you disable JDBC batch updates, Hibernate can check the returned rowcount. The lack of a rowcount is a problem with the JDBC batch update API.


Top
 Profile  
 
 Post subject: Re: Checking an object exists before delete or update
PostPosted: Tue Sep 30, 2003 11:30 am 
Regular
Regular

Joined: Tue Sep 30, 2003 11:27 am
Posts: 60
Location: Columbus, OH, USA
graz wrote:
Secondly, failing the first question, is there a good way to find out whether the object exists in the persistent store before I call these operations. As I will be using the same session, the documentation says I shouldn't try a session.load(Class, Serializable) to confirm existence, and indeed if I try this I get an exception on the following update saying "Another object was associated with this id (the object with the given id was already loaded)"

example code:

Object result = session.load(target.getClass(), target.getId());
session.update(target);


However the documentation says to use find(), which I am assuming is session.find(String). If I build up a query string manually and then use this however I get exactly the same error.

example:

List result = session.find(query);
if (result.size() > 0)
{
session.update(target);
}

Am I doing something monumentally dumb here and does anyone know what I need to write to do this?

Thanks for any help.


This is exactly the same problem I'm having and I just can't seem to solve it. Did you? Anyone else have insight? I read the "Advanced Problems" FAQ about it but how else are you supposed to retrieve an object, make modifications to it and update the thing? I am using collections (and updating them), and I am opening the session just before the update() and closing it right afterward. I'm stumped!
Scott


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