-->
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: Retrieving the joined Entity with a Criteria
PostPosted: Thu Oct 09, 2008 9:52 am 
Newbie

Joined: Thu Oct 09, 2008 7:56 am
Posts: 4
Hi folks,

I've two Entity (called Price and RentalUnit) with a monodirectional ManyToMany relationship.
Here the code:
Code:
@Entity
public class Price {
  @ManyToMany
  @JoinTable(name = "price_rental_unit",
      joinColumns = {@JoinColumn(name = "price_id") },
      inverseJoinColumns = {@JoinColumn(name = "rental_unit_id") }
   )
   @IndexColumn (name = "index_column")
   private List<RentalUnit> rentalUnits;
}

@Entity
public class RentlaUnit {
    ......
}


I'm writing a DAO method using Criteria Hibernate to retrieve all rentalUnits entities that have the same price.
My method would return a List<RentalUnit> from a join between Price and RentalUnit.
I wrote a Criteria like this:
Code:
DetachedCriteria criteria = DetachedCriteria.forClass(Price.class);
criteria.add(Restrictions.idEq(price.getId()));
criteria.createAlias("rentalUnits", "rentalUnit")
   .setProjection(Projections.projectionList().add(Projections.property("rentalUnit.name")));


thus I can retrieve the name of RentalUnit but I would like to retrieve the Entity!
Have you any idea to solve this?
Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 09, 2008 11:14 am 
Expert
Expert

Joined: Mon Nov 26, 2007 2:29 pm
Posts: 443
No need to do a projection.
Apply a result transformer
Code:
criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY)

so that the prices don't duplicate for each child, and then access with java code the getRentalUnits() of each price.

_________________
Gonzalo Díaz


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 10, 2008 6:25 am 
Newbie

Joined: Thu Oct 09, 2008 7:56 am
Posts: 4
Thanks to reply Gonzalo,
I found other solution with a ResultTransformer:

Code:
DetachedCriteria criteria = DetachedCriteria.forClass(Price.class);
criteria.add(Restrictions.idEq(price.getId()));
criteria.createAlias("rentalUnits", "rentalUnit")
   .setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP);

List result = criteria.getExecutableCriteria(delegatedDao.getHibernateSession()).list();
List<RentalUnit> rentalUnits = new ArrayList<RentalUnit>();

for (Object aResult :result) {
   Map map = (Map) aResult;
   RentalUnit rentalUnit = (RentalUnit) map.get("rentalUnit");
   if (!rentalUnits.contains(rentalUnit))
      rentalUnits.add(rentalUnit);
}
return rentalUnits;


thus I must not to use getPrices().
Thanks a lot.


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.