-->
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: saveOrUpdate: excluding column without saying update=false
PostPosted: Tue Mar 28, 2006 12:26 pm 
Newbie

Joined: Tue Mar 28, 2006 12:15 pm
Posts: 2
I have a question similar to this: http://forum.hibernate.org/viewtopic.php?t=951806

When calling saveOrUpdate, I don't want to persist all properties of the object at times while at other times, I like to persist them all.

Specifying update=false in the mapping will result in saveOrUpdate NEVER persisting the property which is undesirable. Is there a way to programatically specify which properties to exclude from persisting right before the call to saveOrUpdate()

I know I can do this by writing HQL or by creating two similar properties and persisting one of them but I'm hoping for a simple solution like:

session.excludeFromUpdate(User.class, "password");

I know this is too ideal but is there anything of the sort?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 29, 2006 12:37 am 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Nope, nothing like that. Hibernate persists objects, not bits of objects, so it's highly unlikely to ever be implemented like that. I know I'd argue against it. If you have fields that you don't always want saved, you could use a one-to-one cascade="none" to the same table, and have a main object and a rarely-saved object, then figure out in business logic when you should save the rarely-saved object.


Top
 Profile  
 
 Post subject: Translate Hibernate inadequacies into design patterns?
PostPosted: Wed Nov 15, 2006 5:48 pm 
Newbie

Joined: Thu Nov 02, 2006 8:26 pm
Posts: 6
In a web application using JSF...

The reasoning behind my insanity:
Maintaining all my entity objects in the http session (or faces saveState) on the front end seems to be an inefficient solution when saving/updating. It also isn't very practical to maintain all the entity properties in the form. Instead, I would like to just pass in what needs updated from the form, instantiate the entity bean within the backing bean (done automatically by faces), leave everything else null, and do the save/update on it for only values that are not null.

How it is an inadequacy- My current options:
1) Persist the whole entity bean in the http session (very bad idea)
2) Pass all of the entity properties to the front end in the form as input text/hidden fields and pass them back to the managed bean (even worse)
3) Before instantiating the saveOrUpdate() method get the current instance from Hibernate and merge the changed properties from the passed instance (lesser of the evils, but is still a bad idea)

The ideal solution:
Just as Hibernate provides the capability to manipulate what properties are returned to the entity bean in a SELECT [Example.create(myEntity).ExcludeZeros().ExcludeProperty(myPropName)], allow a form of Criteria to be added to do the same in an INSERT, UPDATE, or DELETE.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 15, 2006 7:22 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
That's a slightly different question to the one asked before. You can what you need with only a little change. Instead of starting with an empty object, load the object you want to change from the DB using the normal get() or load() method. In your mapping, add the dynamic-update="true" property to the relevant classes. This means that only the updated values will be saved, the others will be omitted from the update statement.

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


Top
 Profile  
 
 Post subject: dynamic-update="true"
PostPosted: Wed Nov 15, 2006 7:31 pm 
Newbie

Joined: Thu Nov 02, 2006 8:26 pm
Posts: 6
Can this be done programmatically rather than using dynamic-update="true"? I only want to do this on an ad hoc basis.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 15, 2006 7:53 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
The ad hoc question is what this topic was originally about. No, it can't be done ad hoc. My earlier suggestion (the one-to-one cascade="none") is still the best pure-hibernate solution, I think.

_________________
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.  [ 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.