-->
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.  [ 5 posts ] 
Author Message
 Post subject: many-to-many associations and subqueries
PostPosted: Fri Aug 19, 2005 8:30 am 
Newbie

Joined: Fri Aug 19, 2005 8:05 am
Posts: 2
Hi
i 've mapped a many-to-many association between 2 classes (Person and Segment) ; all works fine and the generated sql script is :

create table Person (
idPerson varchar(50) not null,
firstName varchar(30) not null,
primary key (idPerson)
);
create table Segment (
segmentId varchar(50) not null,
name varchar(255) not null,
line_index integer,
primary key (segmentId)
);


create table Person_segment_bridge (
idSegments varchar(50) not null, //references Segment (segmentId);
idPersons varchar(50) not null, //references Person (idPerson);
primary key (idPersons, idSegments)
);
...this generated intermediate table is not related to a java object


I'd like to load all person objects from a query that would look like this in sql :
SELECT * for Person WHERE idPerson in (SELECT idPersons FROM Person_segment_bridge psb WHERE psb.idPersons='p1')

How should i achieve this with hibernate ?

Thanxs for help


Top
 Profile  
 
 Post subject: Re: many-to-many associations and subqueries
PostPosted: Fri Aug 19, 2005 9:28 am 
Regular
Regular

Joined: Thu Dec 02, 2004 7:11 am
Posts: 85
Belga wrote:
I'd like to load all person objects from a query that would look like this in sql :
SELECT * for Person WHERE idPerson in (SELECT idPersons FROM Person_segment_bridge psb WHERE psb.idPersons='p1')

How should i achieve this with hibernate ?

Thanxs for help


1) from Person p where p.id='p1' and exists elements(p.segments)

2) from Person p where p.id='p1' and p.segments.size > 0


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 19, 2005 9:48 am 
Newbie

Joined: Fri Aug 19, 2005 8:05 am
Posts: 2
Thxs sergeya but nevermind i wrote it bad : the right sql query is :

SELECT * for Person WHERE idPerson in (SELECT idPersons FROM Person_segment_bridge psb WHERE psb.idSegments='p1')

so it first look up all "p1" segments and then select all persons related to this segment.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Aug 20, 2005 11:38 am 
Regular
Regular

Joined: Thu Dec 02, 2004 7:11 am
Posts: 85
Belga wrote:
Thxs sergeya but nevermind i wrote it bad : the right sql query is :

SELECT * for Person WHERE idPerson in (SELECT idPersons FROM Person_segment_bridge psb WHERE psb.idSegments='p1')

so it first look up all "p1" segments and then select all persons related to this segment.


1) from Person p join p.segments s where s.id='p1'

2) select p from Person p, Segment s where s.id='p1' and p in elements(s.persons)


Top
 Profile  
 
 Post subject:
PostPosted: Sun Aug 21, 2005 5:16 am 
Regular
Regular

Joined: Thu Dec 02, 2004 7:11 am
Posts: 85
3) without HQL, using mapping capability:

Segment s = (Segment)session.get(Segment.class, 'p1');

Set<Person> persons = s.getPersons();


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.