| Author |
hibernate one to one with multiple table
|
Vinay Taneja
Greenhorn
Joined: Apr 04, 2012
Posts: 9
|
|
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
Joined: Mar 07, 2010
Posts: 1282
|
|
You mean inheritance using a discriminator column?
http://docs.jboss.org/hibernate/orm/4.1/manual/en-US/html_single/#inheritance-tablepersubclass-discriminator
|
[How To Ask Questions][Read before you PM me]
|
 |
Vinay Taneja
Greenhorn
Joined: Apr 04, 2012
Posts: 9
|
|
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
Joined: Aug 25, 2009
Posts: 59
|
|
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
Joined: Apr 04, 2012
Posts: 9
|
|
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
Joined: Mar 07, 2010
Posts: 1282
|
|
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
|
 |
 |
|
|
subject: hibernate one to one with multiple table
|
|
|