-->
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.  [ 6 posts ] 
Author Message
 Post subject: simple criteria question
PostPosted: Thu Apr 27, 2006 3:10 pm 
Newbie

Joined: Thu Apr 27, 2006 3:01 pm
Posts: 3
Location: San Francisco, CA
Hello,

I have a simple query:

Code:
select d.projects from Department d where d.id=1


where projects mapped as many-to-many association.

I want to do the same thing using Criteria API, i.e. retrieve list of projects from the association; not a map, just a list of projects.

Please help.

Alexei


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 27, 2006 3:40 pm 
Expert
Expert

Joined: Tue Apr 25, 2006 12:04 pm
Posts: 260
Try this one.

Code:
DetachedCriteria dc = DetachedCriteria.forClass( Project.class , "p" )
                                    .createAlias( "departments", "depts" )
                                    .add( Restrictions.eq( "depts.id", new Integer(1) ) );
List r = dc.getExecutableCriteria( session ).list();


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 27, 2006 3:58 pm 
Newbie

Joined: Thu Apr 27, 2006 3:01 pm
Posts: 3
Location: San Francisco, CA
That's not what the original query expressed.

I cannot navigate back from projects to departments (project does not have references to departments), so your solution won't work.

Alexei


Top
 Profile  
 
 Post subject: Re: simple criteria question
PostPosted: Thu Apr 27, 2006 4:30 pm 
Regular
Regular

Joined: Wed Feb 22, 2006 11:28 am
Posts: 65
Location: Santiago, Chile
[quote="sluramod"]Hello,

I have a simple query:

[code]select d.projects from Department d where d.id=1[/code]

where projects mapped as many-to-many association.

I want to do the same thing using Criteria API, i.e. retrieve list of projects from the association; not a map, just a list of projects.

Please help.

Alexei[/quote]

The Reference Document of Hibernate sayed:

[quote]
You may also specify properties of components or composite user types (and of components of components, etc). Never try to use a path-expression that ends in a property of component type (as opposed to a property of a component). For example, if store.owner is an entity with a component address

store.owner.address.city // okay
store.owner.address // error!
[/quote]

So, u can´t get the List from a HQL senteces. You can do inversely, like this:
from Project p where p.department.id = 1

I guess that will help you to solve your problem.

Requests for comments.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 27, 2006 4:35 pm 
Expert
Expert

Joined: Tue Apr 25, 2006 12:04 pm
Posts: 260
The other way around
Code:
DetachedCriteria dc = DetachedCriteria.forClass( Department.class , "dept" )
                                    .createAlias( "projects", "projs" )
                                    .add( Restrictions.eq( "dept.id", new Integer(1) ) );
List r = dc.getExecutableCriteria( session ).list();


If there is navigation from Department to Projects then why use criteria or HQL query. Instead just loading the Department with its prim key will give you association to projects.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 27, 2006 5:01 pm 
Newbie

Joined: Thu Apr 27, 2006 3:01 pm
Posts: 3
Location: San Francisco, CA
Quote:
So, u can´t get the List from a HQL senteces. You can do inversely, like this:
from Project p where p.department.id = 1


FYI, select d.projects from Department d where d.id=1 works just fine.

Quote:
DetachedCriteria dc = DetachedCriteria.forClass( Department.class , "dept" )
.createAlias( "projects", "projs" )
.add( Restrictions.eq( "dept.id", new Integer(1) ) );
List r = dc.getExecutableCriteria( session ).list();


r contains list of departments in this case, so it is not a solution either.

I know that I can access list of Projects from Department, but I need criteria object for that query, not results of its execution.

_________________
Alexei


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