-->
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.  [ 7 posts ] 
Author Message
 Post subject: subquery 2 levels
PostPosted: Wed Aug 29, 2007 7:01 am 
Newbie

Joined: Sat Feb 26, 2005 11:45 am
Posts: 13
Location: Stockholm, Sweden
Hi!
This is probably a trivial question...

I have a Ticket object which hold Price objects (one-to-Many), Price objects hold PriceLevel objects (one-to-Many).

Trying to figure out how to select all tickets with a specific PriceLevel.pk.

Selecting prices from ticket is no problem.
crit.createAlias("prices", "pricesAlias").
setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY).
add( Restrictions.between("pricesAlias.price", fromPrice, toPrice));

But have struggled with the priceLevels for quite some time.
Would like to do something like this....

crit.createAlias("prices", "pricesAlias").
setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY).
add( Restrictions.between("pricesAlias.price", fromPrice, toPrice));
crit.createAlias("priceAlias", "priceLevelAlias").
setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY).
add( Restrictions.eq("priceLevelAlias.id", pk);

Any pointers?

_________________
kmike


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 29, 2007 7:28 am 
Regular
Regular

Joined: Thu Oct 19, 2006 12:07 pm
Posts: 75
crit.createAlias("prices", "pricesAlias").
setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY).
add( Restrictions.between("pricesAlias.price", fromPrice, toPrice));
crit.createAlias("priceAlias.priceLevels", "priceLevelAlias").
setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY). // this line is superfluous
add( Restrictions.eq("priceLevelAlias.id", pk);


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 29, 2007 7:41 am 
Newbie

Joined: Sat Feb 26, 2005 11:45 am
Posts: 13
Location: Stockholm, Sweden
Nope that won't work, that suggests that there would be a property called priceAlias on ticket.

Base my criteria from a Ticket entity.
Criteria crit = getSession().createCriteria(Ticket.class);
....
....

_________________
kmike


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 29, 2007 10:59 am 
Regular
Regular

Joined: Thu Oct 19, 2006 12:07 pm
Posts: 75
Are you sure ? Did you try ?

Then maybe this would work :
Code:
crit.createAlias("price.priceLevels", "priceLevelAlias").

(this or the first idea work 99%)

And this works 99,999 % ;-)
Code:
crit.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY).
createCriteria("prices").
add( Restrictions.between("price", fromPrice, toPrice));
crit.createCriteria("priceLevels").
add( Restrictions.eq("id", pk);


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 29, 2007 11:14 am 
Regular
Regular

Joined: Thu Oct 19, 2006 12:07 pm
Posts: 75
I just tried both possibilities with setAlias() and they both work (with hibernate version 3.2.0.cr2).


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 30, 2007 9:24 am 
Newbie

Joined: Sat Feb 26, 2005 11:45 am
Posts: 13
Location: Stockholm, Sweden
sweet, thanks for leading me in the right direction.

This worked !!

Criteria crit = getSession().createCriteria(Ticket.class);
crit.createAlias("prices", "pricesAlias").
setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY).
add( Restrictions.gt("pricesAlias.price", 0f));
crit.createAlias("pricesAlias.priceLevels", "priceLevelsAlias").
add( Restrictions.eq("priceLevelsAlias.id", searchCriteria.getPriceLevel().getId()));

_________________
kmike


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 30, 2007 10:52 am 
Regular
Regular

Joined: Thu Oct 19, 2006 12:07 pm
Posts: 75
Don't forget to rate my post ;-)


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