This is a mirror of official site: http://jasper-net.blogspot.com/

Nhibernate query only properties and many-to-one

| Monday, August 23, 2010
  This is how the domain was build, we decided to set a IList<DomainRegistrations> in the NickName class, and make this relation unidirectional. Now I need to issue a query to recover only Id and Url of all NickNameDomainRegistration object belonging to a certain NickName.
The obvious solution is, Load the NickName fetching NickNameDomainRegistration, then grab the information you need. But I do not like this approach, because the Note field can be really long, and I do not want fetching unnecessary data from database. Moreover I have index on database on Id, Url, and the foreign key to nick name, so I really want to issue a projection that only query those fields. The structure of the project is based on ICriteria for issuing query, and I want a simple way to express the query. Thanks to Query only properties I can add this line to the mapping of NickNameDomainRegistration

  1: <many-to-one name="NickName" access="none" class="NickName" column="budr_buniId" />

  Thanks to the access=”none” I can tell nhibernate that NickNameDomainRegistration has a property of type NickName even if the POCO object does not have it. Now I can issue a query for NickNameDomainRegistration with a  condition of “NickName.Id == id”. Everything works fine, but running the whole set of unit test I noticed that some other test has failed.
Examining the tests, I found that association to NickNameDomainRegistration is no more saved into the database. This is due to the fact, that now nhibernate think that the relation should be managed from the <many-to-one> part, even if the <set> in NickName is mapped as Inverse=”true”
The solution is changing the mapping, telling nhibernate that we really do not want the query only property to be used in insertion and update of the relation.

  1: <many-to-one name="NickName" access="none" class="NickName" column="budr_buniId"
  2:                update="false" insert="false"  />

Now all of my tests passes again, and I look at generated query with NHProfiler.

Read more: Alkampfer's Place

Posted via email from .NET Info

0 comments: