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.  [ 6 posts ] 
Author Message
 Post subject: Sorting sub objects
PostPosted: Thu Nov 06, 2008 3:33 pm 
Newbie

Joined: Thu Oct 18, 2007 1:10 pm
Posts: 9
Hi All,

I am having a parent and child relationship as shown below:

Parent Configuration:

<class name="PERSPerson" table="[dbo].[PERS_Person]" optimistic-lock="version" lazy="true">

<bag name="PERSPersonAddress" cascade="all-delete-orphan" inverse="true" lazy="true"
optimistic-lock="false">
<key>
<column name= "`Person_id`" />
</key >
<one-to-many class="PERSPersonAddress" />
</bag >

Child Configuration:

<class name="PERSPersonAddress" table="[dbo].[PERS_PersonAddress]" optimistic-lock="version" lazy="true">
<many-to-one name="PERSPerson" class="PERSPerson" cascade="save-update" outer-join="auto" lazy="false">
<column name="`Person_id`"/>
</many-to-one >


I tried to programmatically sort the sub objects based on the column in the child table as shown below.

ICriteria criteria = GetSession().CreateCriteria(typeof(T));
criteria.CreateAlias("PERSPersonAddress", "a").AddOrder(Order.Asc("a.PersonAddressEnteredDate")) ;

where T is parent type

What I see the SQL Profiler ( I am using Microsoft SQL Server) is a select statament with join statement for Parent and child with the order by clause as expected. When I try to get the child collection property on the Parent, another SQL statement for the child is submitted without the order by clause. I am wondering how to resolve this issue.

Also, I tried to configure the sorting by placing the order-by clause name as part of the parent's mapping file for the person address bag. It worked fine as expected.

Appreciate your help..

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 07, 2008 3:17 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
You marked the bag as lazy="true" which means, the bag isn't initialized with your first query. When you access the property the bag will be lazily loaded, but now there is no order by, because it's not specified in the mapping.

Quote:
Also, I tried to configure the sorting by placing the order-by clause name as part of the parent's mapping file for the person address bag. It worked fine as expected.


I think that's the correct approach.

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 07, 2008 10:16 am 
Newbie

Joined: Thu Oct 18, 2007 1:10 pm
Posts: 9
Thank you.

I thought about that and ran a test with lazy=false. But I don't see any change in the behaviour. Following are the queries submitted to the database.

exec sp_executesql N'SELECT this_.[Person_id] as Person1_78_1_, ... FROM [dbo].[PERS_Person] this_ inner join [dbo].[PERS_PersonAddress] a1_ on this_.[Person_id]=a1_.[Person_id] WHERE this_.[Person_id] = @p0 ORDER BY a1_.[PersonAddressEnteredDate] asc',N'@p0 bigint',@p0=1258

exec sp_executesql N'SELECT persperson0_.[Person_id] as Person3___1_, persperson0_.[PersonAddress_id] as PersonAd1_1... FROM [dbo].[PERS_PersonAddress] persperson0_ WHERE persperson0_.[Person_id]=@p0',N'@p0 bigint',@p0=1258

I think that the resultset that I get as part of the first query are overwritten by the second query. Any thoughts about that?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 07, 2008 10:24 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Try

Code:
ICriteria criteria = GetSession().CreateCriteria(typeof(T), "t");
     .SetFetchMode("t.PERSPersonAddress", FetchMode.Join)
     .AddOrder(Order.Asc("t.PERSPersonAddress.PersonAddressEnteredDate")) ;

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 07, 2008 10:44 am 
Newbie

Joined: Thu Oct 18, 2007 1:10 pm
Posts: 9
Thank you. I tried the code

ICriteria criteria = GetSession().CreateCriteria(typeof(T), "t");
criteria.Add(Expression.IdEq(id));
criteria.SetFetchMode("t.PERSPersonAddress", FetchMode.Join);
criteria.AddOrder(Order.Asc("t.PERSPersonAddress.PersonAddressEnteredDate"));

and get the following exception

NHibernate.QueryException: could not resolve property: PERSPersonAddress.PersonAddressEnteredDate of: BusinessEntities.PERSPerson.

If I replace the last line with
criteria.CreateAlias("PERSPersonAddress", "a").AddOrder(Order.Asc("a.PersonAddressEnteredDate"));

I end up with the behaviour that I stated in my earlier post.


Top
 Profile  
 
 Post subject: What is the HQL to order on a collection field?
PostPosted: Tue Dec 30, 2008 12:42 pm 
Regular
Regular

Joined: Tue Feb 19, 2008 6:05 pm
Posts: 82
How do we order on a collection field, let it be a set or bag as list is usually obtained by the listIndex that we explicity specify?

How about the HQL? Sorry, it is the same.

from ParentObj parentObj
left join fetch parentObj.children child
order by child.childField

Thanks
Krishna


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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.