-->
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.  [ 2 posts ] 
Author Message
 Post subject: Does Nhibernate supports bulk saving ?
PostPosted: Thu Dec 13, 2007 5:27 am 
Newbie

Joined: Mon Jul 16, 2007 9:35 am
Posts: 17
Hi all,

I am currently working on NHibernate 1.2 for my Project.
I was able to save my transaction using save(caseObject) for Single Entity.If i try the same for entity collection save(caseObjectCollectionb), I am unable to achieve it.

The reason behind targeting this bulk insert, is to minimise the hit to the database.By getting support to bulk insert(say 5 or 15 records) the number of hits to the database is reduced to 1 rather than depending on the number of records.

Here with I have provided samples.

Normal Save Entity:

public virtual Result Save(Case objCase)
{
try
{
this.OpenSession();
this.BeginTransaction();
session.Save(objCase);

this.CommitTransaction();
return new Result(true);
}
catch (Exception ex)
{
return new Result(false,ApplicationManager.StackExceptionMessages(ex),ex);
}
finally
{
}
}

Saving an EntityCollection:

public virtual Result Save(CaseCollection objCaseCollection)
{
try
{
this.OpenSession();
this.BeginTransaction();

session.Save((IList)objCaseCollection);

this.CommitTransaction();
return new Result(true);
}
catch (Exception ex)
{
this.RollbackTransaction();
return new Result(false, ApplicationManager.StackExceptionMessages(ex), ex);
}
finally
{
}
}

When i try to save this EntityCollection, I am getting this ERROR
"Unknown Entity Class".


Please give me your thoughts, what i am missing or wrong.
Let me know any further clarification needed.

Thanks
Cheers
Sudhagar.S


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 13, 2007 8:57 am 
Senior
Senior

Joined: Thu Feb 09, 2006 1:30 pm
Posts: 172
This may sound really strange, but NHibernate doesn't work like you probably think. Save doesn't mean write insert now to the database. Instead it means associate a new transient object with the session so that when the flush occurs that item will be persisted to the database.

Now, the strange part of this is that it is not 100% true when you are using identity columns for the primary key. When using an identity column it will actually make the call to the database since the Save operation requires a primary key for the object. If your object is using a client side generated id such as a Guid then it would not need to make the call to the database.

When you call CommitTransaction it actually implicitly calls Flush for you (this is an option). When the Flush is called all objects being saved would then be saved in bulk.

So in order to accomplish this (assuming you are saving new objects and not objects which were loaded from the database since there Save will not do ANYTHING). Just loop over each object calling Save on it, then flush the transaction, all items will be saved in bulk. If you are using identity inserts I don't think there is anything you can do. There may be something I am not aware of here, but that has been my experience.


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