-->
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: Unidirectional OneToMany reverse?
PostPosted: Fri Mar 25, 2011 10:10 am 
Newbie

Joined: Tue Oct 19, 2010 9:18 am
Posts: 7
Hello!

I have the following 2 Entities (shortened)

Code:
public class User {
   @Id @GeneratedValue
   protected long id;
}

Code:
public class Lieferant {
   @Id @GeneratedValue
   protected long id;

   @ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
   @JoinTable(
      joinColumns=@JoinColumn(name="lieferantid", referencedColumnName="id"),
      inverseJoinColumns=@JoinColumn(name="userid", referencedColumnName="id")
   )
   private Set<User> users = new HashSet<User>();
}

now i want to figure out, which User are NOT in any Lieferant.users.
The corresponding SQL sould be like this:
SELECT * FROM Users WHERE id NOT IN (SELECT userid FROM Lieferanten_Users)

so far my closest approach to get there was this:
Code:
SELECT u FROM User u WHERE u NOT IN (SELECT l.users FROM Lieferant l)
---- generated (not working) SQL:
select
  user0_.id as id130_
from
  dbo.Users user0_
where
  user0_.id not in  (
   select
    .                                // here is the error: just a dot instead of user3_.id
   from
    dbo.Lieferanten lieferant1_,
    dbo.Lieferanten_Users users2_,
    dbo.Users user3_
   where
    lieferant1_.id=users2_.lieferantid
    and users2_.userid=user3_.id
  )

also not working:
Code:
SELECT u FROM User u WHERE u.id NOT IN (SELECT l.users.id FROM Lieferant l)
> illegal attempt to dereference collection [lieferant1_.id.users] with element property reference [id]
why "lieferant1_.id.users" and not "lieferant1_.users.id"?

how to do it right?

Chris


Top
 Profile  
 
 Post subject: Re: Unidirectional OneToMany reverse?
PostPosted: Fri Mar 25, 2011 2:21 pm 
Newbie

Joined: Tue Oct 19, 2010 9:18 am
Posts: 7
i got it work, but now i am confused...

SELECT u FROM User u WHERE u NOT IN (SELECT l.users FROM Lieferant l)
SELECT u FROM User u WHERE u NOT IN (SELECT lu FROM Lieferant l, IN (l.users) lu)

The first statement selects the List / Set? This would explain why i cannot compare it to Entity "User".
And the second Statement selects the Items IN the List/Set?!

Chris


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.