Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

hibernate one to one with multiple table

 
Vinay Taneja
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I want to create a relationship on one table with multiple tables based on value in another column. How can I do that in hibernate.
eg:

Rejections table has two columns ref_id and ref_type and based on the value of ref_type, it will look for in different tables. if ref_type is "soft" it will look into softwares table for ref_id, if ref_type is "service" it will look into services table for same ref_id.

basically table A has one reference column and on reference type column (descriptor column). Table A joins to Table B, Table C and Table D on same reference columns but based on descriptor column value. Table B/C/D all are distinct entities of different type and have no relation logically and domain wise. Item created by Table A will have has a relationship with these tables. I want to fetch Table A records based on some joins which can join A with any of B/C/D.

its like
select * from (
select A.* from table A,table B where A.reference=B.id and B.somecolumn='abcdefg'
UNIOIN
select A.* from table A,table C where A.reference=C.id and C.somecolumn='xyz')
order by 1
 
Bill Gorder
Bartender
Posts: 1682
7
Android IntelliJ IDE Linux Mac OS X Spring
 
Vinay Taneja
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is not IS A relation between tables A/B/C/D it is simple adhoc join where I want to UNION output of any number of queries that will be data driven. Also A/B/C/D entities are all independent they can have have HAS A relation with others but not IS A.
it is not simple case of descriptor column because no inheritance.

I know UNION and a lot of simple stuff is not possible in hibernate but that is what I need with dynamic where clause and JOIN with having order by on result of UNION.
 
RamandeepS Singh
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Vinay

I think Bill Gorder is right. You can achive it using Discriminator column with Inheritance type= Joined

Regards
Ramandeep Singh
 
Vinay Taneja
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As I said it is not IS A relation but has a relation, it is not subclassing. A is complete by itself and does not need any data from B/C/D but table A has foreign key column which can have value from any of B/C/D. I need to select records of A type based on some where condition on B/C/D. Also it can be many unions at runtime
its like
select * from (
select A.* from table A,table B where A.reference=B.id and B.somecolumn='abcdefg'
UNIOIN
select A.* from table A,table B where A.reference=B.id and B.someOthercolumn=1245
UNION
select A.* from table A,table C where A.reference=C.id and C.yetAnothercolumn='xyz')
order by 1

now in above eg. I joined A and B twice, at any runtime it can be joined many times with different where condition. Also A has its own primary key id.
 
Bill Gorder
Bartender
Posts: 1682
7
Android IntelliJ IDE Linux Mac OS X Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok. Sorry I read your post a bit quickly. If you are looking to make a union query using hibernate it is not supported. You will either have to do this with native SQL or you can look at alternatives.
http://stackoverflow.com/questions/201023/hibernate-union-alternatives
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic