-->
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 record via <sql-update> or with sql query
PostPosted: Wed Jul 25, 2007 4:40 am 
Newbie

Joined: Mon Jun 25, 2007 11:35 am
Posts: 15
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

NHibernate version:
1.2

Mapping documents:
<sql-query name="updateTag">
UPDATE TAGS SET TAGID = :tagID, MATRICOLA = :matricola, DATA = GetDate()
FROM TAGS
WHERE TAGID = :oldTagID
</sql-query>


Name and version of the database you are using:
MS SQL SERVER 2005

Hallo. i would like to update a record on a table named Tags via a custom sql query.

As first way i tried to use the
<sql-update>UPDATE PERSON SET NAME=UPPER(?) WHERE ID=?</sql-update>

but in could not found any example on how to use this way from the code.

So I have tried namedQuery as the one above but i cannot find a way to execute it like in Hibernate, the only way to execute is:

IQuery query = session.GetNamedQuery("updateTag");
query.SetParameter("tagID", "9999999999999999");
query.List();

And obviously i get the exception
NHibernate.QueryException: Return types of SQL query were not specified

Any idea which is the simplest way to update a record using an HQL/SQL query?

Kind regards

Massimo


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 25, 2007 11:00 am 
Hibernate Team
Hibernate Team

Joined: Tue Jun 13, 2006 11:29 pm
Posts: 315
Location: Calgary, Alberta, Canada
The simplest way to update a record (reads "entity") is to Get() the entity, change the property that you want to update, then Update() it.

_________________
Karl Chu


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 25, 2007 3:56 pm 
Newbie

Joined: Mon Jun 25, 2007 11:35 am
Posts: 15
Ok, i Know that the simplest way to update an entity is via the nhibernate API (aka SaveOrUpdate() ), but since the property that i want to update is the primary key (I know i should not update the primary key etc. etc.... but this is what i HAVE to do....) i have to update it via a SQL query or an HQL query.

Any idea which is the simplest way? (named query or sql-update in the mapping file..)

kind regards

Massimo


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 25, 2007 4:05 pm 
Hibernate Team
Hibernate Team

Joined: Tue Jun 13, 2006 11:29 pm
Posts: 315
Location: Calgary, Alberta, Canada
To update the primary key (not recommended, as you noted), the simplest way is to fall back to ADO.NET.

Code:
IDbCommand cmd = session.Connection.CreateCommand();
// nhTransaction.Enlist(cmd); // if you are in a transaction
...
cmd.Execute();

_________________
Karl Chu


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 26, 2007 3:04 am 
Newbie

Joined: Mon Jun 25, 2007 11:35 am
Posts: 15
Ok, so your suggestion is go back to ADO.Net.

It's strange because i found on the reference this chapter.

13.3. Custom SQL for create, update and delete, and i thought that was the right place to make my custom update.

...
<sql-update>UPDATE PERSON SET NAME=UPPER(?) WHERE ID=?</sql-update>
...

The problem was tha I didn't know and understand how to call the sql-update from the code.
Is this operation triggered during the normal object Save() ?

Kind regards

Massimo


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jul 28, 2007 6:03 pm 
Hibernate Team
Hibernate Team

Joined: Tue Jun 13, 2006 11:29 pm
Posts: 315
Location: Calgary, Alberta, Canada
The <sql-update> element is used so you can use a custom update statement instead of the NHibernate-generated update statement. I think NHibernate only passes "the ID" to the SQL statement; it does not pass an "old ID" and "new ID". Therefore, I don't think you can use <sql-update> to change the primary key. I can be wrong on this though.

_________________
Karl Chu


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.