-->
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.  [ 4 posts ] 
Author Message
 Post subject: Object arrays from list
PostPosted: Tue Jan 13, 2004 11:30 am 
Senior
Senior

Joined: Sun Jan 04, 2004 2:46 pm
Posts: 147
Assume I have a dead simple hibernate object with two fields, ID and Name, ID being an int primary key, Name a string.

I run a query to get all the rows in the table, and use toArray() function of list to return all the objects, modify one of them and flush the session. Presumably hibernate has no way of knowing I have made a change because the objects I'm working on are copies made by the call to toArray(). Is this right and is there a way of getting ALL the hibernate objects from a list so that implicit updates work?

The code.

Code:
List list = session.find( "from MyObjects" );
MyObject[] objects = (MyObject[])list.toArray();
objects[ 0 ].setName();
session.flush();


Is the alternative to iterate and use get( i )?

Cheers.

Myk.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 13, 2004 11:34 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
what's wrong with doing l.get(i) ?

and besides i do not think toArray() clones the objects - it just creates a new array and make it's entires point to the actual objects.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 13, 2004 11:40 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
IMHO

Code:
MyObject o = session.createQuery("from MyObject").uniqueResult();
o.setSomething(xxx);

is the most convenient


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 13, 2004 12:49 pm 
Senior
Senior

Joined: Sun Jan 04, 2004 2:46 pm
Posts: 147
max wrote:
what's wrong with doing l.get(i) ?

and besides i do not think toArray() clones the objects - it just creates a new array and make it's entires point to the actual objects.


Indeed. I assumed that toArray() and eventually System.arrayCopy() would clone the objects but it appears from the native code that just the references are copied.

Cheers guys.

Myk.


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