tags:

views:

254

answers:

3

Is there a way in NHibernate to start with an unproxied model

var m = new Model() { ID = 1 };
m.Name = "test";
//Model also has .LastName and .Age

Now save this model only updating Name without first selecting the model from the session?

A: 

http://www.hibernate.org/hib_docs/nhibernate/html/mapping.html

dynamic-update (optional, defaults to false): Specifies that UPDATE SQL should be generated at runtime and contain only those columns whose values have changed.

Place dynamic-update on the class in the HBM.

var m = new Model() { ID = 1 };
m = session.Update(m); //attach m to the session.
m.Name = "test";
session.Save(m);
bleevo
I am not sure at this time if .Update hits the database, I do not want it too.
bleevo
Try it. Most probably, update doe NOT hit the database immediately. It just puts the model to the session and marks it as dirty that it will be stored anyway.
Stefan Steinegger
dynamic-update does not work in this case, since NH does not have the original values and does not know what has changed. See this question: http://stackoverflow.com/questions/1243390/what-is-the-best-approach-to-update-only-changed-properties-in-nhibernate-when-se/1243739#1243739
Stefan Steinegger
+1  A: 

If model has other properties then name, you need to initialize these with the original value in the database, unless they will be set to null.

You can use HQL update operations; I never tried it myself.

You could also use a native SQL statement. ("Update model set name ...").

Usually, this optimization is not needed. There are really rare cases where you need to avoid selecting the data, so writing this SQL statements are just a waste of time. You are using an ORM, this means: write your software object oriented! Unless you won't get much advantages from it.

Stefan Steinegger
A: 

What Stefan says looks like what you need. Please be aware that this is really an edge case and you should be happy with fully loading your entity unless you have some ultra-high-performance issues.

If you simply don't want to hit the database - try using caching - entity cache is very simple and efficient.

If your entity is a huge one - i.e. it contains a blob or something - think about splitting it in two (with many-to-one so that you can utilize lazy loading).

Rashack