Reputation: 875
Is there a way to have NHibernate only update the fields that don't have the default value filled in? Say we have this simple class:
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
}
We also have a standard NHibernate mapping using properties for each field and Id for the Id field.
Say we want a page to just update the Name, so we send down the Id, store it in a hidden field, and show a text box for the name. The user modifies the name and hits save. We POST back, new up a Person object, and assign the incoming Id and Name. We then tell NHibernate to update the database.
Problem is, the Age obviously gets set back to 0 in the database as it wasn't filled in coming back from the POST. The most obvious way around this is to send the Age value to the page, store it in a hidden field, and fill it back in before saving. That works fine until you get quite a few more properties on your object and only want to modify a hand full of them on the screen (or a large list of Person objects, which might require storing lots of values in hidden fields).
So back to the subject of the question: is there a way to tell NHibernate to basically ignore the Age property since it'd have a default value (0) before being updated, or am I stuck with a metric ton of hidden fields to keep the existing values?
Upvotes: 0
Views: 110
Reputation: 1366
In the post back, Retrieve the persisted entity from the repository with the Id and update just the Name and persist it. This way, you don't need to maintain the values in hidden field.
Upvotes: 1
Reputation: 88345
This sounds a little suspicious. Normally, when you go to edit a Hibernate entity, you first load it from the database, make changes to it, then save it back to the database. You wouldn't normally create a new object, set its ID and whatever arbitrary properties, then save the new object, because, as you say, any properties that are not set will most likely be set to defaults.
By loading it from the database, all the properties are filled in appropriately, so you don't have to worry about maintaining the state of these yourself. The Hibernate session is responsible for maintaining the state of these objects until you save and flush them to the database.
There are different strategies for dealing with the hibernate session across web requests. Check out this article: http://hibernatebp.blogspot.com/ - look at session management strategies. "Session per request with detached objects" might be good in your case.
Upvotes: 0