-->
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.  [ 9 posts ] 
Author Message
 Post subject: Persisting a Collection in Hibernate
PostPosted: Wed Jun 21, 2006 1:54 am 
Beginner
Beginner

Joined: Fri Jun 16, 2006 7:47 am
Posts: 27
In Hibernate to update,
we set the values in the POJO classes and then do Session.save(POJO);

However, say we have to update a collection of these POJO's.
To be more clear, I have in my jsp, rows of data which I have to update.

How do I update these rows in one shot.
Else, I have to have a for loop and then set the POJO each time and then do a save.

I want something like Session.save (Collection of POJO).

Is this possible or what is the alternative?

Any help will be deeply appreciated.

Thanks.


Top
 Profile  
 
 Post subject: Re: Persisting a Collection in Hibernate
PostPosted: Wed Jun 21, 2006 4:53 am 
Beginner
Beginner

Joined: Sun Jul 10, 2005 12:11 pm
Posts: 32
Location: Hertfordshire, England
cooler8020002000 wrote:
In Hibernate to update,
we set the values in the POJO classes and then do Session.save(POJO);

However, say we have to update a collection of these POJO's.
To be more clear, I have in my jsp, rows of data which I have to update.

How do I update these rows in one shot.
Else, I have to have a for loop and then set the POJO each time and then do a save.

I want something like Session.save (Collection of POJO).

Is this possible or what is the alternative?

Any help will be deeply appreciated.

Thanks.


If the collection is mapped with
Code:
cascade="save-update"
then you can just:

Code:
session.save(Parent object of collection)


So say if you had a cat, which contains a collection of kittens, you could use
Code:
session.save(cat)


Please see http://www.hibernate.org/hib_docs/reference/en/html_single/#example-parentchild-collections and the rest of the documentation on collections (playing with cats is a useful example).

HTH!

_________________
--
Christian Ashby
Spiralinks, Inc.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 21, 2006 5:08 am 
Beginner
Beginner

Joined: Fri Jun 16, 2006 7:47 am
Posts: 27
Hi,

I should have made this a bit more clear.

eg: we have a Employee pojo with Emp#, EmpName.

Now in our view page, we have a series of rows having Employee details (Emp#, EmpName).

If I have to update 1000 rows, it would mean I have to invoke Session.save(EmployeePOJO) a 1000 times.

Can I put these Emp Details in an ArrayList and then do a one time Session.save or something else so that it would be a one time operation?

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 21, 2006 5:20 am 
Beginner
Beginner

Joined: Sun Jul 10, 2005 12:11 pm
Posts: 32
Location: Hertfordshire, England
cooler8020002000 wrote:

Can I put these Emp Details in an ArrayList and then do a one time Session.save or something else so that it would be a one time operation?



I don't know of a way to do this; calling .save whilst iterating through such a list is the easiest option.

Don't forget that this doesn't necessarily immediately run the update statements, depending on your batch size settings, so the call to .save() is relatively quick.

Optimising these settings for your case is probably a case of looking through the debug output to see when the updates occur and what time cost they have.

HTH.

_________________
--
Christian Ashby
Spiralinks, Inc.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 21, 2006 5:54 am 
Beginner
Beginner

Joined: Fri Jun 16, 2006 7:47 am
Posts: 27
Thanks cscashby. I feel performance will improve if we are doing a batch operation than doing Session.save(POJO) say a 1000 times.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 21, 2006 7:00 am 
Beginner
Beginner

Joined: Sun Jul 10, 2005 12:11 pm
Posts: 32
Location: Hertfordshire, England
cooler8020002000 wrote:
Thanks cscashby. I feel performance will improve if we are doing a batch operation than doing Session.save(POJO) say a 1000 times.


That's exactly my point! session.save() doesn't do the save until:

a) The transaction / connection is committed
b) The batch is 'full' ie the batch size is reached

So this _is_ a batch operation, you just don't explicitly deal with the batching (hibernate does it for you)

_________________
--
Christian Ashby
Spiralinks, Inc.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 21, 2006 7:17 am 
Beginner
Beginner

Joined: Sun Jul 10, 2005 12:11 pm
Posts: 32
Location: Hertfordshire, England
Double-post deleted!

_________________
--
Christian Ashby
Spiralinks, Inc.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 21, 2006 9:32 am 
Regular
Regular

Joined: Mon May 08, 2006 6:00 am
Posts: 53
Location: India
why dont you use plaing update in HQL which will fire one single query at DB updating your 1000 rows

e.g

session.createQuery("update TableRean tb set tbcolumn=updatedval where tb.tbcolumn='whereconditio'").executeupdate();

yuu dont have to use session.update(Object) method every time

see if it helps to you

thanks

sudhir

-----------------------------------------
pls rate if it solves your problem :-)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 21, 2006 2:21 pm 
Beginner
Beginner

Joined: Fri Jun 16, 2006 7:47 am
Posts: 27
Thanks again cscashby for your reply.

Sudhir, unfortunately, I can't use the update statement as my updates are not that general (using where clause for multiple updates).

Guess, I would have to go with Session.save for every record.

Thank you all you good people and God bless!


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