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.  [ 4 posts ] 
Author Message
 Post subject: Update selective columns
PostPosted: Sat Aug 19, 2006 11:35 pm 
Newbie

Joined: Sat Aug 19, 2006 10:32 pm
Posts: 4
Is it possible to update selective columns in the database?

Let's say I have a User class with the following properties:
ID
Name
Phone

Let's say that I want to change the Phone. The pesudo code is
user.Phone = newPhone;
session.Update(user);

This works.

But let us say that I have a business rule that the Phone may not be updated together with the Name. The client programmer may inadvertently make a coding mistake like this:

user.Phone = newPhone;
user.Name = newName;
session.Update(user);

This would be a violation of the business rule because it will update both Name and Phone fields at the same time. How can I ignore the Name change and only update Phone?

It would appear that it can be done this way as described in Chaper 13 of Hibernate doc like below. Unfortunately, NHibernate does not yet implement the executeUpdate() method.

Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();

String hqlUpdate = "update Customer c set c.name = :newName where c.name = :oldName";
// or String hqlUpdate = "update Customer set name = :newName where name = :oldName";
int updatedEntities = s.createQuery( hqlUpdate )
.setString( "newName", newName )
.setString( "oldName", oldName )
.executeUpdate();
tx.commit();
session.close();

_________________
--strato


Top
 Profile  
 
 Post subject:
PostPosted: Sun Aug 20, 2006 8:49 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
You cannot get NHibernate to ignore a column. You'd have to do that using raw SQL.

You can fake it by using this pseudocode:
Code:
tempPhone = user.Phone;
session.Refresh(user);
user.Phone = tempPhone;
session.Update(user);
Just as an FYI, you can put dynamic-update="true" on the class mapping to get hibernate to update only the changed columns. This won't help you ignore certain changes, it just reduces the amount of output SQL, at the cost of dynamically generating the update SQL.

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Aug 20, 2006 11:12 pm 
Newbie

Joined: Sat Aug 19, 2006 10:32 pm
Posts: 4
You cannot get NHibernate to ignore a column. You'd have to do that using raw SQL.

By raw SQL, do you mean direct access to the database outside of NHibernate?


You can fake it by using this pseudocode:
tempPhone = user.Phone;
session.Refresh(user);
user.Phone = tempPhone;
session.Update(user);


Thanks. I will try this.

_________________
--strato


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 21, 2006 12:35 am 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Direct SQL access using the NHibernate connection. In Hibernate, it would be using session.connection(); I don't know what the equivalent in NHibernate is.

_________________
Code tags are your friend. Know them and use them.


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