-->
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.  [ 6 posts ] 
Author Message
 Post subject: update TABLE set x='test' where y>1 and y<100 --> P
PostPosted: Fri Sep 28, 2007 8:47 am 
Newbie

Joined: Fri Sep 28, 2007 8:40 am
Posts: 4
I want to update some columns in all rows with the matching criteria.

Is this possible with NHibernate 1.2? I searched the documention but couldn't find anything...

Please don't tell me the only way to do this, is query for all the items and then update them one by one.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 28, 2007 9:16 am 
Hibernate Team
Hibernate Team

Joined: Tue Jun 13, 2006 11:29 pm
Posts: 315
Location: Calgary, Alberta, Canada
Batch updating is not NHibernate's strength. You might be better off issuing a raw SQL statement using ADO.NET directly. The next question that seems to come up a lot is "how do I tie the IDbCommand into the NH-managed transaction?" The answer to that is ITransaction.Enlist().

_________________
Karl Chu


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 28, 2007 10:53 am 
Newbie

Joined: Fri Sep 28, 2007 8:40 am
Posts: 4
Thanks for the information.

I haven't done anything directly with ADO.NET (only via NHibernate) and here is what i tried:

Code:
ITransaction trans = _session.BeginTransaction();

System.Data.IDbCommand cmd = _session.Connection.CreateCommand();
cmd.CommandText = "update Patient set FirstName='Maximilian' where LastName='Mustermann'";
               
trans.Enlist(cmd);
trans.Commit();
_session.Flush();


Unfortunatly this doesn't work. I activated SQL Output and don't see the statement being executed and the database hasn't changed too.

Any hints what I'm doing wrong.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 28, 2007 11:29 am 
Hibernate Team
Hibernate Team

Joined: Tue Jun 13, 2006 11:29 pm
Posts: 315
Location: Calgary, Alberta, Canada
Anything you do with ADO.NET does not have anything to do with NHibernate anymore; except it execute in the same transaction created by NHibernate. Also, the CommandText needs to be raw SQL, System.Data.IDbCommand knows nothing about NHibernate and the mapping files.

By the way, no need to Flush() after trans.Commit(); in fact, it may cause problem. So you may want to take that out.

_________________
Karl Chu


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 01, 2007 2:37 am 
Newbie

Joined: Fri Sep 28, 2007 8:40 am
Posts: 4
karlchu wrote:
Anything you do with ADO.NET does not have anything to do with NHibernate anymore; except it execute in the same transaction created by NHibernate. Also, the CommandText needs to be raw SQL, System.Data.IDbCommand knows nothing about NHibernate and the mapping files.


I'm aware of that. It's raw SQL, but the problem is, that it will not be executed!

karlchu wrote:
By the way, no need to Flush() after trans.Commit(); in fact, it may cause problem. So you may want to take that out.


Removed it --> nothing changed.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 01, 2007 3:00 am 
Newbie

Joined: Fri Sep 28, 2007 8:40 am
Posts: 4
I solved the problem...

NHibernate of course doesn't execute the command, this must be done manually:

Code:
ITransaction trans = _session.BeginTransaction();

System.Data.IDbCommand cmd = _session.Connection.CreateCommand();
cmd.CommandText = "update Patient set FirstName='Maximilian' where LastName='Mustermann'";
               
trans.Enlist(cmd);

cmd.ExecuteNonQuery();

trans.Commit();


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