Win a copy of GANs in ActionE this week in the AI forum
or WebAssembly in Action in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

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

 
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.

 
A teeny tiny vulgar attempt to get you to buy our stuff
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!