-->
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.  [ 7 posts ] 
Author Message
 Post subject: Left outer join on tables without relationship
PostPosted: Sun Sep 12, 2010 8:45 pm 
Newbie

Joined: Sun Sep 12, 2010 8:29 pm
Posts: 6
Hi...I am newbie to hibernate. I have this problem with outer join. I have two tables table1 and table2:
Table1:
ID
field1
field2

Table2
ID
field3
field4

There is no relationship from tables and I want perform this SQL join:
select * from Table1 left outer join Table2 on Table1.field1=Table2.field4

How can I do this with HQL if in the mapping there is no relation between field1 in Table1 and field4 on Table2?
Sorry for my bad english. Thanks a lot.


Top
 Profile  
 
 Post subject: Re: Left outer join on tables without relationship
PostPosted: Mon Sep 13, 2010 1:49 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
HQL works on the object model, not on database tables. In other words, if there is no mapped association you can't perform a join with HQL.


Top
 Profile  
 
 Post subject: Re: Left outer join on tables without relationship
PostPosted: Mon Sep 13, 2010 8:37 am 
Newbie

Joined: Sun Sep 12, 2010 8:29 pm
Posts: 6
Thanks nordborg.
So if I want make an outer left join I must create a relation between entities? For example a many to many relation between field1 and field4? And in this case, can exist an object of Table2 with field4 that have a value that not exist in a object of Table1? In other words, with a relation between that fields I can have a table2 object with a value of field4 that haven't a corresponding value of field1 in a object of entity Table1?


Top
 Profile  
 
 Post subject: Re: Left outer join on tables without relationship
PostPosted: Mon Sep 13, 2010 5:58 pm 
Regular
Regular

Joined: Sun Feb 14, 2010 3:29 pm
Posts: 58
Location: USA
@tegolo

As nordborg said, you need mapped your entities first. HQL does allow you to perform OUTER JOIN after you have corrected mapped the associations, if that's what you asking. The fact that you said two tables have fields that must equals, they are "related". Read the user manual and try HQL out on your own. repost if you have specific error with code sample.

Also, Hibernate supports native SQL query if you must resort to that. See doc for details.

_________________
Zemian Deng
------------
Need a Java Scheduler? Try
http://bitbucket.org/timemachine/scheduler


Top
 Profile  
 
 Post subject: Re: Left outer join on tables without relationship
PostPosted: Tue Sep 14, 2010 2:54 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Quote:
For example a many to many relation between field1 and field4? And in this case, can exist an object of Table2 with field4 that have a value that not exist in a object of Table1?


I don't see how this can be a many-to-many relation in Hibernate terms. It usually requires a join table that holds the relation. Is this some kind of legacy database that you can't change? If so, it may be that using native SQL is your best option. Otherwise I suggest that you redesign the tables.


Top
 Profile  
 
 Post subject: Re: Left outer join on tables without relationship
PostPosted: Tue Sep 14, 2010 4:31 am 
Newbie

Joined: Sun Sep 12, 2010 8:29 pm
Posts: 6
Yes, it is an existing database that I prefer not change and my experience with Hibernate is little...specially my little knowledge of Hibernate is the problem! :-)
I need some fields in a table and I have the necessary to take other fileds. I can find these fields in another table but...there isn't a really relatioship between the tables in classic terms, in logical terms there is a relation between datas but no relationship in DBMS....In other words there is a bad design of DB because field1 and field4 aren't keys...logically I can make Table1 and Table2 in a single table...but this is that I have. I need some fields of Table2 for a PDF report...and these fields are only in that table. I could put interesting fields in Table2 into Table1...but in native SQL I can get datas for my export with a left outer join using field1 and field4 though fields aren't key using: "ON TABLE1.FIELD1=TABLE2.FIELD4" and I thought have the same thing somehow in HQL. But maybe the solution is redesign DB tables or using native SQL.
Anyway thanks a lot nordborg and saltnlight5.


Top
 Profile  
 
 Post subject: Re: Left outer join on tables without relationship
PostPosted: Tue Sep 14, 2010 4:44 am 
Newbie

Joined: Sun Sep 12, 2010 8:29 pm
Posts: 6
If I have necessary of a inner join I can change query in:

Query myQuery= em.createQuery("SELECT new MyJoin(t1.field1,t1.field2,t2.field3,t2.field4) FROM Table1 t1, Table2 t2 WHERE t1.field1=t2.field4");
List myList= query.getResultList();

But in left outer join I don't know an alterantive method.
Thanks.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 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.