-->
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.  [ 3 posts ] 
Author Message
 Post subject: Help with Criteria to get projection prop from collection
PostPosted: Thu Apr 27, 2006 8:56 pm 
Regular
Regular

Joined: Thu Jul 29, 2004 11:55 pm
Posts: 75
Hi,

We are using Hibernate 3.1.x. I am trying to get a list of properties instead of a collection using Criteria. When I run the form I have, no sql gets executed and there are no errors. can someone help me figure out the criteria or let me know whether it is possible or not.

DetachedCriteria criteria;
long parentId = 5;

//create the criteria for the list of properties
criteria = DetachedCriteria.forClass(Children.class)
.setProjection(Projections.property("name"))
//create the sub criteria
.createCriteria("parent")
//add the parent id restriction to the sub-criteria (Parent)
.add(Restrictions.eq("parentId", parentId));

Here is my current criteria. I am trying to get a list of names stored in the child collection by passing in the parent id. Parent -> Children is a one-to-many relationship. For simplicity (not real code), here is the simple classes

public class Parent {
public long parentId;
public Set children;
}

public Children {
public Parent parent;
public String name;
}

Thanks for any help in advance.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 28, 2006 12:12 am 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
You're doing it backwards. Try this:
Code:
criteria = DetachedCriteria.forClass(Parent.class);
criteria.add(Restrictions.eq("parentId", parentId"));
criteria.createAlias("children", "c");
criteria.setProjection(Projections.property("c.name"));

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 02, 2006 7:29 pm 
Regular
Regular

Joined: Thu Jul 29, 2004 11:55 pm
Posts: 75
Thanks, that worked.


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