• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

[Hibernate] Join without FK Relationship using Criteria

 
Freddy Wong
Ranch Hand
Posts: 959
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have this problem to perform a join between two tables when there is no FK relationship at all. I've googled it and spent countless hours with no luck

Suppose I have these two entities.


With HQL, I can simply do this.


Is there a way to achieve the same result using Query by Criteria? And I need to get the properties from both entities. So, using in subquery will not work at all.

Thanks in advance.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think your only option is to do it as two queries and join in code. or just use HQL.

I would ask however, why are you trying to treat unrelated data as if it were related? If Person has an addressID why does it not have a proper association with the Address table? Hibernate tends not to work well when you start trying to do stuff that kind of goes against the logic of relational data. If these entities are really related and you haven't enforced this relationship in your data model it is oly a matter of time before the data becomes corrupt.
 
Freddy Wong
Ranch Hand
Posts: 959
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply.

It's just for my curiosity because a friend of mine asked me this and it sometimes happen when we deal with legacy apps. Btw, wouldn't the performance be rather bad if we perform the join in code as opposed to in HQL?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic