-->
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.  [ 7 posts ] 
Author Message
 Post subject: Saving all collection of objects with one command?
PostPosted: Sun Apr 09, 2006 9:55 am 
Beginner
Beginner

Joined: Mon Feb 13, 2006 8:34 pm
Posts: 27
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:3.0

Is this possible to save all collection of objects using one command (does something like "saveAll" exits for hibernate) or it is necessary to make it by using save() in a e.g. for loop?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 10, 2006 1:02 am 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
There is limited support for bulk operations in HQL. See section 13.4. "DML-style operations" of the ref docs.

You can also write your own bulk-updating code, which would save each object and flush the transaction after all the saves are called. Read all of chapter 13 for more suggestions.


Top
 Profile  
 
 Post subject: getting the same problem
PostPosted: Mon Apr 10, 2006 9:31 am 
Newbie

Joined: Thu Apr 06, 2006 4:04 pm
Posts: 9
I am getting the same problem? I went through chapter 13 but this didn't answer my question. How can I save or update a Collection in the database. Can I just use saveOrupdate(Collection)??Please Help.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 10, 2006 9:41 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
No, you can't do that. You can save each item individually (optimizing the process through judicious use of Session.flush), possibly using stateless sessions. If you can do the updates via a simple SQL statement (e.g. "update table set value = value + 1 where parentid = :parentid") then you can use DML-style updates:
Code:
String hqlUpdate = "update Table t set t.value = t.value + 1 where t.parent = :parent";
Query qry = session.createQuery( hqlUpdate );
qry.setParameter( "parent", parent);
qry.executeUpdate();
This is described in section 13.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 11, 2006 5:56 am 
Beginner
Beginner

Joined: Mon May 02, 2005 6:17 pm
Posts: 41
It's not very hard to implement such a mechanism. It's even listed in reference manual.
Here is my implementation

Code:
protected void saveAll(List data)
   {
      Session session = null;
      
      try
      {
         session = getSession();
         for(int i=0;i<data.size();i++)
         {
            Object object = data.get(i);
            session.save(object);
            if(i != 0 && i % BATCH_SIZE == 0)
               session.flush();

         }
         
         session.flush();
      }
      catch (Throwable e)
      {
         throw new RuntimeException(e.getMessage(),e);
      }
      finally
      {
         if(session != null)
            releaseSession(session);
      }
   }


Session if flushed on every BATCH_SIZE-th element so there is no risk of out of memory error.

_________________
www.globalresearch.ca


Top
 Profile  
 
 Post subject: can use single command for collection
PostPosted: Wed Apr 19, 2006 3:13 pm 
Newbie

Joined: Thu Apr 06, 2006 4:04 pm
Posts: 9
yes it is possible to use a single command. and it is working for me.

public void setXXX(Set set) {
Transaction tx = session.beginTransaction();
Iterator iterator = set.iterator();
while (iterator.hasNext())
{
XXX rec= (XXX)iterator.next();
session.saveOrUpdate(rec);
}
tx.commit();
}

Set of given records are saved here.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 19, 2006 5:48 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
That's not a single command: you're still calling saveOrUpdate once per item in the colleciton. This is essentially a less-refined version of Skinner's earlier suggestion, though at least your version doesn't have the must-be-avoided "catch Throwable".


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