This week's giveaway is in the Programmer Certification (OCPJP) forum.
We're giving away four copies of Java Mock Exams (software) and have David Mayer on-line!
See this thread for details.
Win a copy of Java Mock Exams (software) this week in the Programmer Certification (OCPJP) forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Sorting on a property of an inner object (object within an object)

 
Prem Vinodh
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a User object which has birth information which is an Event object.
I am trying to sort the information on the eventDate property of the event birth and having some problem.

The hbm files are as follows:-
============================

<hibernate-mapping>
<class name="com.church.domain.User" table="User">

<id name="id" column="ID" type="java.lang.Long"
unsaved-value="0">
<generator class="increment" />
</id>

<property name="surName" column="SUR_NAME" not-null="true" />

<property name="firstName" column="FIRST_NAME" />

<property name="lastName" column="LAST_NAME" />

<many-to-one name="birth" class="com.church.domain.Event" lazy="false" fetch="join"
column="BIRTH" unique="true" cascade="save-update" />

</class>


<class name="com.church.domain.Event" table="EVENT" lazy="false">

<id name="id" column="ID" type="java.lang.Long"
unsaved-value="0">
<generator class="increment" />
</id>

<property name="eventName" column="EVENT_NAME" />

<property name="eventDate" column="EVENT_DATE" />

<property name="eventPlace" column="EVENT_PLACE" />

</class>

In the DB I have the following rows of information (for simplicity I am
mentioning only the first name and eventDate :

User --- Date Of Birth
=========================
User1 1st Jan 2009
User2 10th Oct 2007
User3
User4


I am using the Criteria API as follows :

sortColumn = "birth.eventDate";

Criteria criteria = getSession().createCriteria(klass);
Criteria subCriteria = criteria.createCriteria("birth");
subCriteria.addOrder(Order.asc("eventDate"));

This fetches only 2 rows :

User1 1st Jan 2009
User2 10th Oct 2007

And I guess it is internally using equi join and hence gets only 2 rows.

Whereas I want all the 4 rows to be displayed... so I did the following

sortColumn = "birth.eventDate";

Criteria criteria = getSession().createCriteria(klass);
criteria.setFetchMode("birth", FetchMode.EAGER);
Criteria subCriteria = criteria.createCriteria("birth");
subCriteria.addOrder(Order.asc("eventDate"));

and still I get only 2 rows and not the 4 rows.

User1 1st Jan 2009
User2 10th Oct 2007

When I modify the criteria as follows :

sortColumn = "birth.eventDate";

Criteria criteria = getSession().createCriteria(klass);
criteria.setFetchMode("birth", FetchMode.EAGER);

I get all the 4 rows :

User1 1st Jan 2009
User2 10th Oct 2007
User3
User4

but they are not sorted on the date of birth (eventDate property of birth).

I want to get all the 4 records and all the 4 records should be sorted on the date of birth (eventDate property of birth).

Any help would be appreciated
Thanks in advance.

 
Prem Vinodh
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a User object which has birth information which is an Event object.
I am trying to sort the information on the eventDate property of the event birth and having some problem.

The hbm files are as follows:-
============================


In the DB I have the following rows of information (for simplicity I am
mentioning only the first name and eventDate :

User --- Date Of Birth
=========================
User1 1st Jan 2009
User2 10th Oct 2007
User3
User4

I am using the Criteria API as follows :



This fetches only 2 rows :

User1 1st Jan 2009
User2 10th Oct 2007

And I guess it is internally using equi join and hence gets only 2 rows.

Whereas I want all the 4 rows to be displayed... so I did the following



and still I get only 2 rows and not the 4 rows.

User1 1st Jan 2009
User2 10th Oct 2007

When I modify the criteria as follows :



I get all the 4 rows :

User1 1st Jan 2009
User2 10th Oct 2007
User3
User4

but they are not sorted on the date of birth (eventDate property of birth).

I want to get all the 4 records and all the 4 records should be sorted on the date of birth (eventDate property of birth).

Any help would be appreciated
Thanks in advance.

 
Happiness is not a goal ... it's a by-product of a life well lived - Eleanor Roosevelt. Tiny ad:
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!