-->
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: simple criteria query
PostPosted: Mon Feb 02, 2009 3:13 am 
Newbie

Joined: Mon Feb 02, 2009 3:03 am
Posts: 3
Hi,

Following from the hibernate examples, let's say I have a list of Cats each containing a list of kittens with the date they were born (column "born" on kittens table) and their name. Let's say I want to query all Cats that have a kitten where I want to check that only the last Kitten born has a name equals to "Mimi"

This is what I thought would work but doesn't:

Code:
Criteria c = session.createCriteria(Cats.class).createCriteria("kittens").
                        addOrder(Order.desc("date")).setMaxOrder(1).
                        add(Restrictions.eq("name", "Mimi"));
                return c.list();
            }


Any ideas are greatly appreciated!

Thanks, Jason[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 02, 2009 4:08 am 
Senior
Senior

Joined: Thu Jan 08, 2009 3:48 pm
Posts: 168
I think you need to turn the order around:

Code:
Criteria c = session.createCriteria(Cats.class)
.createCriteria("kittens").add(Restrictions.eq("name", "Mimi"))
.addOrder(Order.desc("date")).setMaxResults(1);
return c.list();


Otherwise the "name" restriction will apply to the Cat, not the kitten. Not 100% sure but i had some problem like this a while ago.
Also it should be setMaxResults instead of setMaxOrder but i guess that was a typo


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.