These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 posts ] 
Author Message
 Post subject: Lazyly load bags from NamedQuery
PostPosted: Tue May 27, 2008 8:03 pm 
Newbie

Joined: Tue May 27, 2008 7:47 pm
Posts: 5
Hi all

It's the first time I'm using nHibernate in an ASP.NET project. We've got a Parent-Child relationship between 3 tables, let's call them parent, child1, child2.

In the parent class, I defined two bags for the one-to-many relationship (lazy=true), and the corresponding many-to-one mapping in the children. one of the children class has some properties that don't come directly from the child table, but from another inner-joined table:

(SQL)
SELECT
c.id, c.create_date, c.type_code,
t.type_description
FROM child c
inner join types t on (c.type_code = t.type_code)


So, I had to defined a <sql-query> to get the child data since a simple query.list(of child) cannot get the property type_description.

But the problem is, when I query the parent, nHibernate gets the data fine, but when I try to access the child list, I get an error for "lazy load exception", because nHibernate cannot find the corresponding "type_descripcion" field in the child table.

How do I tell nHibernate to use named query by default in a lazy-load of children?


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 28, 2008 2:22 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Have you specified the query in the collection mapping ? Like (from reference doc, chapter 13.4):

Code:
<set name="Employments" inverse="true">
<key/>
<one-to-many class="Employment"/>
<loader query-ref="employments"/>
</set>

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 29, 2008 11:10 am 
Newbie

Joined: Tue May 27, 2008 7:47 pm
Posts: 5
thanx for your reply.

I've been playing with that <loader> tag, finally found out that I have to use session.Load() instead of session.CreateQuery() in order to have nHibernate use the named query to load the entity.

However, I have to use explicitly the session.Load() method to load the entity, but when I lazy load the child, it is done automatically by nHibernate, so, how can I tell nHibernate to "load" the child using the named query?

thanx


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 29, 2008 11:15 am 
Newbie

Joined: Tue May 27, 2008 7:47 pm
Posts: 5
thanx for your reply.

I've been playing with that <loader> tag, finally found out that I have to use session.Load() instead of session.CreateQuery() in order to have nHibernate use the named query to load the entity.

However, I have to use explicitly the session.Load() method to load the entity, but when I lazy load the child, it is done automatically by nHibernate, so, how can I tell nHibernate to "load" the child using the named query?

thanx


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 02, 2008 10:26 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Can you post your named query ?

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 09, 2008 12:41 pm 
Newbie

Joined: Tue May 27, 2008 7:47 pm
Posts: 5
Hi

here's my code:



<class name="Entities.Parent, Entities" table="PARENT" >
<id name="ParentId" column="PARENT_ID" type="System.Int32">
<generator class="assigned"/>
</id>
<property name="Name" column="NAME" access="field.camelcase-underscore"
not-null="false" type="System.String" insert="true" update="true"/>
<property name="ParentTypeCode" column="TYPE_CODE" access="field.camelcase-underscore"
not-null="false" type="System.String" insert="true" update="true"/>
<property name="ParentTypeName"
not-null="false" type="System.String" insert="false" update="false"/>
<bag name="Children" table="CHILD" cascade="all" lazy="true" inverse="true">
<key column="ParentId" />
<one-to-many class="Entities.Child, Entities" />
<loader query-ref="LoaderChild"/>
</bag>

<loader query-ref="LoaderParent"/>
</class>



<class name="Entities.Child, Entities" table="CHILD" >
<id name="ChildId" column="CHILD_ID" type="System.Int32">
<generator class="assigned"/>
</id>
<many-to-one name="Father" column="PARENT_ID" access="field.camelcase-underscore"
not-null="true" outer-join="auto"></many-to-one>
<property name="Name" column="NAME" access="field.camelcase-underscore"
not-null="false" type="System.String" insert="true" update="true"/>
<property name="ChildTypeCode" column="TYPE_CODE" access="field.camelcase-underscore"
not-null="false" type="System.String" insert="true" update="true"/>
<property name="ChildTypeName"
not-null="false" type="System.String" insert="false" update="false"/>

<loader query-ref="LoaderChild"/>
</class>




<sql-query name="LoaderParent">
<return alias="Parent" class="Entities.Parent, Entities" />
<![CDATA[
SELECT
p.PARENT_ID as {Parent.ParentId},
p.TYPE_CODE as {Parent.ParentTypeCode},
t.TYPE_NAME as {Parent.ParentTypeName},
FROM PARENT e
LEFT OUTER JOIN ENTITY_TYPE t ON (p.TYPE_CODE = t.CODE)
WHERE p.PARENT_ID = ?
]]>
</sql-query>


<sql-query name="LoaderChild">
<return alias="Child" class="Entities.Child, Entities" />
<![CDATA[
SELECT
c.PARENT_ID as {Child.ChildId},
c.TYPE_CODE as {Child.ChildTypeCode},
t.TYPE_NAME as {Child.ChileTypeName},
FROM CHILD c
LEFT OUTER JOIN ENTITY_TYPE t ON (c.TYPE_CODE = t.CODE)
WHERE c.PARENT_ID = ?
]]>
</sql-query>



Dim p1 As New Parent
p1 = sess.Load(GetType(Entities.Parent), parent_id)


When I execute the line, it loads the parent correctly, but when I try to access the children (Paren.Children), it tells me that it has a null reference

What's wrong with my code?

thank you very much


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 10, 2008 2:22 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Akaik, you can't use the same query for entity and collection loading. For a collection query you need something like this:

Code:
<sql-query name="employments">
     <load-collection alias="emp" role="Person.Employments"/>
...
</sql-query>


Code:
sql-query name="LoaderChildCollection">
     <load-collection alias="Child" role="Parent.Childs"/>
<![CDATA[
SELECT
c.PARENT_ID as {Child.ChildId},
c.TYPE_CODE as {Child.ChildTypeCode},
t.TYPE_NAME as {Child.ChileTypeName},
FROM CHILD c
LEFT OUTER JOIN ENTITY_TYPE t ON (c.TYPE_CODE = t.CODE)
WHERE c.PARENT_ID = ?
]]>
</sql-query>

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 10, 2008 12:46 pm 
Newbie

Joined: Tue May 27, 2008 7:47 pm
Posts: 5
It works!!!!

Thank you very much!!!!

:)


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.