-->
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.  [ 2 posts ] 
Author Message
 Post subject: Trying to Order results with Criteria object w/ a twist...
PostPosted: Wed Sep 09, 2009 4:07 pm 
Newbie

Joined: Wed Sep 09, 2009 3:52 pm
Posts: 2
Hello all,

I'm new to Hibernate, Java developer for 7+ years. I've got two Hibernate books that I have reviewed and started working with. I am writing code that is trying to do the following:

Open a transaction
Create criteria with restrictions
Order by... BUT the order by that I need to order by is VoterHistory.election.electionDate's date
In the class VoterHistory, I have the following:
{
long id;
Voter voter;
Election election;
VoteMethod voteMethod;
}

Code Snippet:
Transaction tx = session.beginTransaction();
try {
Voter voter = (Voter) session.get(Voter.class, voterId);
if(voter == null){
throw new DAOException("Invalid Voter ID: " + voterId);
}

Criteria criteria = session.createCriteria(VoterHistory.class);
criteria.add(Restrictions.eq("voter", voter));
// correct??
criteria.addOrder(Order.desc("election.electionDate"));

if(voterHistoryExample != null){
Example example = Example.create(voterHistoryExample);
example.enableLike();

criteria.add(example);
}

List<VoterHistory> voterHistories = criteria.list();


************ Question
How can I access the VoterHistory.election.electionDate in order to sort by date? Do I need to change or add information to the .hbm.xml mapping file?

Thanks
Phil


Top
 Profile  
 
 Post subject: Re: Trying to Order results with Criteria object w/ a twist...
PostPosted: Mon Sep 14, 2009 1:26 pm 
Expert
Expert

Joined: Sat Jan 17, 2004 2:57 pm
Posts: 329
Location: In the basement in my underwear
Are you getting something along the lines of an invalid property exception?

IIRC, you may need to create an alias to the election table and use that to order.

Something like

criteria.createAlias("election", "electionAlias");
criteria.addOrder(Order.desc("electionAlias.electionDate"));

This would create an inner join to the election table and then the property should be available.

_________________
Some people are like Slinkies - not really good for anything, but you still can't help but smile when you see one tumble down the stairs.


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