-->
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.  [ 9 posts ] 
Author Message
 Post subject: How to use joins between unrelated entities
PostPosted: Mon Mar 07, 2011 11:27 pm 
Newbie

Joined: Thu Oct 21, 2010 5:15 am
Posts: 8
I have a case where there is ParentEntity and a ChildEntity. There is no inheritance defined between both child & parent, though the existence of child totally depends on parent. And parent can exist without child even. And also there is no containment of each other in each entity even.

In this case I wrote a hql named-query like:

select new BusinessDTO(parent.att1, parent.att2, child.attr1, child.attr2) from ParentEntity parent, ChildEntity child where parent.id=? and child.id = parent.id

but this query didn't solved my purpose, my business scenario wanted all above defined parentInfo as well as optional childInfo in the case there is an existing child.

Somehow I tried to use hibernate outer or any type of joins but it failed for me. I can't change my entities all I can do is to write a named-query which can work. Can anyone suggest a solution to this?


Top
 Profile  
 
 Post subject: Re: How to use joins between unrelated entities
PostPosted: Thu Mar 10, 2011 5:13 am 
Regular
Regular

Joined: Fri Jan 28, 2011 11:44 am
Posts: 117
Hi,

If the 2 entities are not linked through any kind of mapping then you cannot join them ...
All you can do is a cross join which is exactly what you did in the HQL query you showed below.


Top
 Profile  
 
 Post subject: Re: How to use joins between unrelated entities
PostPosted: Thu Mar 10, 2011 5:53 am 
Newbie

Joined: Thu Oct 21, 2010 5:15 am
Posts: 8
The only relationship is ChildEntity.id is foreign key to ParentEntity.id, rest there is no other relationship defined in Entity. So overmeulen you say that this is not possible through HQL.


Top
 Profile  
 
 Post subject: Re: How to use joins between unrelated entities
PostPosted: Thu Mar 10, 2011 6:01 am 
Regular
Regular

Joined: Fri Jan 28, 2011 11:44 am
Posts: 117
If you want to use a join in an HQL query you need to specify a OneToOne (or OneToMany ...) relationship between ParentEntity and ChildEntity ...


Last edited by overmeulen on Thu Mar 10, 2011 6:54 am, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: How to use joins between unrelated entities
PostPosted: Thu Mar 10, 2011 6:49 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
Actually if child.id is foreign key to parent.id it is a One-To-One-Relationship. Why don't you define it in your mapping?

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
 Post subject: Re: How to use joins between unrelated entities
PostPosted: Thu Mar 10, 2011 11:49 am 
Newbie

Joined: Thu Oct 21, 2010 5:15 am
Posts: 8
Well, we have mappings defined as annotations on top of entities, and as I said we are in this phase of our project that we cannot change the entity. This is the reason why problem anyway rose.


Top
 Profile  
 
 Post subject: Re: How to use joins between unrelated entities
PostPosted: Thu Mar 10, 2011 12:02 pm 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
then you have to do a cross join as you already did.

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
 Post subject: Re: How to use joins between unrelated entities
PostPosted: Thu Mar 10, 2011 2:42 pm 
Newbie

Joined: Thu Oct 21, 2010 5:15 am
Posts: 8
Well for time now to have outer join similar solution I've changed it to:

select new BusinessDTO(parent.att1,
parent.att2,
(select child.attr1 from ChildEntity child where child.id = parent.id),
(select child.attr2 from ChildEntity child where child.id = parent.id))
from ParentEntity parent
where parent.id=?

This gives me all the results irrespective child exists or not. The only downside I see is that I've to query the ChildEntity twice to get 2 fields of it.


Top
 Profile  
 
 Post subject: Re: How to use joins between unrelated entities
PostPosted: Mon Mar 14, 2011 3:54 pm 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
Why not
Code:
select new BusinessDTO(parent.att1, parent.att2, child.attr1, child.attr2) from ParentEntity parent, ChildEntity child where parent.id=? and (child.id = parent.id or child.id is null)

_________________
-----------------
Need advanced help? http://www.viada.eu


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