Are there any shortcuts to modelling lighterweight versions of my NHibernate mapped model classes?
I have a model class with many properties that is mapped to the database using NHibernate.
If I want to create a lighter weight version of the same class that is only used to retrieve and update one of the properties a开发者_Python百科re there any shortcuts that NHibernate provides or do I just have to create a new class with a new NHibernate mapping file just for the one property that I am interested in?
The reason I want to do this is for database performance, I dont want to be retrieving 10 pieces of data each time when I know that I am only going to update one specific column.
Use HQL:
session.CreateQuery("update TheEntity set TheProp = :newValue where id = :id")
.SetParameter("newValue", theValue)
.SetParameter("id", id)
.ExecuteUpdate();
No need for hacks.
There aren't any shortcuts for this. You'd have to map the separate class with it's own mapping. But you might be putting the cart before the horse. NHibernate has a lot of built in performance tuning, not the least of which is the 1st and 2nd level caching that mitigates much of the issue you're trying to resolve. Assuming you're making use of lazy loading features, updating a single property on an entity is a fairly cheap transaction in NHibernate.
The performance hit of populating the full object instead of one property is (in almost all cases) negligible to nonexistent. Don't worry about it.
Thanks everyone, in the end we have gone with creating cut down versions of our model classes and the our dba's are satisfied.
精彩评论