Hello,
I have a problem getting NHibernate to do what I want it to do. Please look at the end of this post to see one of the the mapping documents I have. Note the
m_items collection as well as the
m_unreadItems property that follows right after. These are the ones that are causing my grievance.
First when I load an instance of this class and I try just to get the number of items in the list, this causes the whole list to be fetched (using single "select ... " statements for each item on the list - ouch, I
have to be doing something wrong here. Any ideas ?
Second the calculated property
m_unreadItems is causing the NHibernate to fail with ADOException("Failed to execute query") apparently when trying to load instances of InfoCore.Category. Please, have in mind that this select is not what I really mean, but rather me trying to get the formula thing to work at all. (To get the first issue, I had to comment out this property so it won't trouble NHibernate)
I would really appreciate any ideas!
Regards,
Petar
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp
NHibernate version: 1.0.2
Mapping documents:
Code:
<class name="InfoCore.Category, InfoCore" table="category">
<id name="m_id" column="id" type="Int32" access="field" unsaved-value="0">
<generator class="native" />
</id>
<discriminator column="cat_type" type="String"/>
<property name="m_name" access="field" />
<many-to-one name="m_parent" class="InfoCore.FolderCategory" access="field" not-null="true" cascade="save-update"/>
<list name="m_items" access="field" table="category_item" cascade="save-update" lazy="true" >
<key column="category_id" foreign-key="id"/>
<index column="idx"/>
<many-to-many class="IItem" column="item_id"/>
</list>
<property name="m_unreadItems" access="field" type="Int32" formula="(select count(elements(cate.m_items)) from category cate where cate.id = id)" />
</class>
Code between sessionFactory.openSession() and session.close():
Full stack trace of any exception that occurs:
Name and version of the database you are using:MSSQL Express 2005
The generated SQL (show_sql=true):NHibernate: SELECT m_categori0_.m_parent as m_parent__, m_categori0_.id as id__, m_categori0_.idx as idx__, m_categori0_.id as id0_, m_categori0_.cat_type as cat_type0_, m_categori0_.m_parent as m_parent0_, m_categori0_.m_name as m_name0_, m_categori0_.m_channel as m_channel0_, (select count(elements(cate.m_items)) from category cate where cate.id = m_categori0_.id) as f0_0_ FROM category m_categori0_ WHERE m_categori0_.m_parent=@p0
@p0 = '1'
Debug level Hibernate log excerpt: