-->
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: Join two coloums
PostPosted: Tue Oct 20, 2015 9:49 am 
Newbie

Joined: Fri Oct 02, 2015 6:11 am
Posts: 6
Hi. I have a problem. I have two tables and i want connected table1'id with id of table2'id.
After i want take the rows of the table2 where the state in the row ( with same id) is "active".

In the class of table1 :
Code:
@NotNull
private Integer numberId;
private State state;


In the class of table2 :
Code:
@ManyToOne(cascade = { CascadeType.REFRESH }, fetch = FetchType.LAZY,optional=true)
@JoinColumn(name = "numeroAco",updatable=false,insertable=false)
private Table1 table1;


In the dao :
Code:
crit.setFetchMode("table1",FetchMode.SELECT);
crit.createAlias("table1", "ta",JoinType.LEFT_OUTER_JOIN);
crit.add(Restrictions.eq("ta.state", "active"));


i try the debugging. In the criteria there is a ac.state=active but hibernate doesn't do that and it do a other filter ( i don't understand the filter)

Help!! what is the problem? thanks !!!!! :(


Top
 Profile  
 
 Post subject: Re: Join two coloums
PostPosted: Wed Oct 21, 2015 8:27 am 
Newbie

Joined: Tue Sep 29, 2015 9:01 am
Posts: 2
SELECT t2.table1_id, t2.id AS table2_id, t2.table3_id_1, t2.table3_id_2
, t31.value AS x
, t32.value AS y
FROM table2 t2
LEFT JOIN table3 t31 ON t31.id = t2.table3_id_1
LEFT JOIN table3 t32 ON t32.id = t2.table3_id_2;

There is no need to join in table1. table2 has all you need - assuming there is a foreign key constraint guaranteeing referential integrity (all t2.table1_id are actually present in table1). Else you may want to join to table1, thereby selecting only rows present in table1.

I use LEFT [OUTER] JOIN (and not [INNER] JOIN) to join to both instances of table3 for a similar reason: it is unclear whether referential integrity is guaranteed. If not, JOIN would drop rows from the result where no matching row is present in table3. I assume you would rather display such rows with a NULL value for any missing x or y.

And table3.id needs to be UNIQUE, or we might multiply rows with several matches from each LEFT JOIN.


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.