No, it /is/ a bad thing to do, for a number of subtle reasons. Hibernate
"wants" you to give it back the same object, complete with the original
collections, because they carry around snaphot information. Hibernate
will usually let you get away with not doing this, but it is certainly
inefficient and results in lots of database traffic as Hibernate attempts
to re-synch with the database.

Don't do it.

Change your application design so that you are using an object from a
previous transaction. This is almost always possible.


Troy McKinnon wrote:


Gavin,

Sorry to 'steam you up'

But I am trying to get to the bottom of this 'without' using hibernate
'wrong'.

The exists method isn't quite what I am looking for - like you said -
saveOrUpdate would accomplish the same.
My issue is a little different. I don't have a object from a previous
transaction; if that was the case I wouldn't have an issue.
Completely Understand That.

And what I am trying to do is probably breaking the concept, but I don't
think so.

Really all I want is to be able to do the same thing with a manually built
object.

i.e. I can do it manually by getting the object1 from transaction, then
using BeanUtils (or similar) to copy all the values from my object to the
'loaded' one.  But it just seems like a messy solution, especially in a
loop.

It is your framework, so its your call. But it doesn't seem to me a BAD
thing to be able to use an object not gotten via a hibernate transaction and
us it with a hibernate transaction.  What if there was some none Hibernate
controller method etc that return a MyObject value that I wanted to use and
persist in my Hibernate implemention.  If the object didn't already exist in
the session I don't see a problem with having Hibernate 'check' if it exists
and USE this object as if it had been loaded.


Cheers


Troy
----- Original Message ----- From: "Gavin King" <[EMAIL PROTECTED]>
To: "Troy McKinnon" <[EMAIL PROTECTED]>
Cc: <[EMAIL PROTECTED]>
Sent: Sunday, November 02, 2003 8:09 PM
Subject: Re: [Hibernate] updating exiting objects with a new clone





Grrrr I hate this. I think it almost always indicates that you are
using  Hibernate "wrong". You should be able to do these kind of things
using saveOrUpdate(); thats what its there for. Get the object in the
/previous/ transaction, set its state, and then call saveOrUpdate().

Now, I can incredibly easily implement a Session.exists() method, but I
think it would encourage bad practice.

You can implement it yourself, of course, by doing a query like:

s.find("select mi.id from MyItem mi where mi.id = ?", id,
Hibernate.LONG).size()==0


Troy McKinnon wrote:




http://forum.hibernate.org/old/914765.html talks around the issue I am
having but it doesn't really solve anything.


We start with an object manually created.


MyItem myItem = new MyItem(id, name, desc);


I then check to see if it already exists.  To determine if I want to
do an update or a save.



MyItem fromDatabase = (MyItem)session.load(MyItem.class,


dblItem.getId());

If fromDatabase existed, I want to update it with the data from myItem.
Because you can't just do an update(myItem) since fromDatabase is
already loaded.
(Note: you can't do a update(myItem) even if you hadn't called the
load - because the session doesn't recognise it to update.)
The only way I can see doing this now is to transfer the field values
from myItem to fromDatabase.
(Note: using the reflect package to do this is fairly trivial - but
just more work )
If there was some way to updateSimilar or updateFromClone or similar
method that would allow an update with a different object, that would
be nice to have.



Cheers

Troy







-------------------------------------------------------
This SF.net email is sponsored by: SF.net Giveback Program.
Does SourceForge.net help you be more productive? Does it
help you create better code? SHARE THE LOVE, and help us help
YOU! Click Here: http://sourceforge.net/donate/
_______________________________________________
hibernate-devel mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/hibernate-devel





------------------------------------------------------- This SF.net email is sponsored by: SF.net Giveback Program. Does SourceForge.net help you be more productive? Does it help you create better code? SHARE THE LOVE, and help us help YOU! Click Here: http://sourceforge.net/donate/ _______________________________________________ hibernate-devel mailing list [EMAIL PROTECTED] https://lists.sourceforge.net/lists/listinfo/hibernate-devel

Reply via email to