I'm trying to optimize the performance of database operations for a typical
parent-child entities scenario. In my case I would like to delete all
children and update the parent's record timestamp in one call to database.
I'm only able to do it using Native SQL (which I would like to avoid):
private static void ClearUsingNativeSql(ISession session, ParentItem
p)
{
var q1 =
session.CreateSQLQuery("delete from Child_Items where
Parent_id = :parentId1")
.SetParameter("parentId1", p.Id)
.FutureValue<int>();
var q2 =
session.CreateSQLQuery("update Parent_Items set timestamp =
:timestamp where parent_item_Id = :parentId2")
.SetParameter("parentId2", p.Id)
.SetParameter("timestamp", DateTime.Now)
.FutureValue<int>();
_log.Info("Runing native SQL queries");
var dummy = q2.Value;
}
When I'm trying to use HQL with MultiQuery / Future queries, I'm getting
the following null reference exception:
System.NullReferenceException: Object reference not set to an instance of
an object.
at NHibernate.Impl.MultiQueryImpl.AggregateQueriesInformation()
at NHibernate.Impl.MultiQueryImpl.get_Parameters()
at NHibernate.Impl.MultiQueryImpl.CreateCombinedQueryParameters()
at NHibernate.Impl.MultiQueryImpl.List()
at NHTest1.Program.ClearUsingMultiQuery(ISession session, ParentItem p)
in c:\Projects\Test\NHTest\NHTest1\Program.cs:line 238
at NHTest1.Program.<>c__DisplayClassf.<BatchHqlTest>b__b(ISession
session) in c:\Projects\Test\NHTest\NHTest1\Program.cs:line 165
at NHTest1.Program.RunInTransaction(Action`1 action, ISession session)
in c:\Projects\Test\NHTest\NHTest1\Program.cs:line 332
at NHTest1.Program.BatchHqlTest(String[] args) in
c:\Projects\Test\NHTest\NHTest1\Program.cs:line 152
Here is the code, I'm using:
private static void ClearUsingMultiQuery(ISession session,
ParentItem p)
{
var q1 = session
.CreateQuery("delete from ChildItem c where c.Parent =
:parentId1")
.SetParameter("parentId1", p.Id);
var q2 = session
.CreateQuery("update ParentItem p set p.Timestamp =
:timestamp where p.Id = :parentId2")
.SetParameter("timestamp", DateTime.Now)
.SetParameter("parentId2", p.Id);
var q = session.CreateMultiQuery().Add(q1).Add(q2);
_log.Info("Runing multiplqueriy");
var dummy = q.List();
}
or
private static void ClearUsingFuture(ISession session, ParentItem p)
{
var q1 = session
.CreateQuery("delete from ChildItem c where c.Parent =
:parentId1")
.SetParameter("parentId1", p.Id)
.FutureValue<int>();
var q2 = session.
CreateQuery("update ParentItem p set p.Timestamp =
:timestamp where p.Id = :parentId2")
.SetParameter("parentId2", p.Id)
.SetParameter("timestamp", DateTime.Now)
.FutureValue<int>();
_log.Info("Runing multiquery");
var dummy = q2.Value;
}
Digging into NH source code I found that it might be a problem of
multi-queries - the framework does not accept non-select queries.
Is the MultiQuery meant to be used for updates too? Or is it a bug I
should report on NHibernate Jira?
Thanks for any advice.
--
You received this message because you are subscribed to the Google Groups
"nhusers" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/nhusers.
For more options, visit https://groups.google.com/d/optout.