-->
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: Outer join with Critera
PostPosted: Fri Oct 10, 2008 7:05 am 
Newbie

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

I've two Entity (called Price and Room) with a monodirectional (from Price to Room) ManyToMany relationship.
Here the code:

Code:
@Entity
public class Price {
  @ManyToMany
  @JoinTable(name = "price_room",
      joinColumns = {@JoinColumn(name = "price_id") },
      inverseJoinColumns = {@JoinColumn(name = "room_id") }
   )
   @IndexColumn (name = "index_column")
   private List<Room> rooms;
}

@Entity
public class Room {
    ......
}


I would do an OUTER JOIN with Criteria but I can't get it.
I want to retrieve all Room entities that have no Price associated
In SQL it'd be:

Code:
SELECT p.id as price_id, ro.id as room_id
FROM room ro LEFT OUTER JOIN price_room pro ON ro.id = pro.room_id
LEFT OUTER JOIN price p ON p.id = pro.price_id
WHERE p.id is null


I wrote the criteria but it produces an INNER JOIN:

Code:
DetachedCriteria criteria = DetachedCriteria.forClass(Price.class);
criteria.add(Restrictions.isNull("id"))
criteria.createAlias("rooms", "room")


Do you know how to make OUTER JOIN?
Thanks a lot


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 10, 2008 7:54 am 
Newbie

Joined: Thu Oct 09, 2008 7:56 am
Posts: 4
I achieved make an outer join..
That is:

Code:
List result =  session.createCriteria(Price.class)
         .createAlias("rooms", "room", JoinFragment.RIGHT_OUTER_JOIN)
                   .setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP)
                   .list();


thus I can get all rooms, even if they haven't price.
But I want to retrieve olny the rooms that haven't price, and so I've added a Restriction to the Criteria:

Code:
.add(Restrictions.isNull("id"))


but it retrieves all price that have rooms associated.
Could someone help me?
I hope was clear explaning it


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.