• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

hibernate one to one with multiple table

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 1682
7
Android Mac OS X IntelliJ IDE Spring Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You mean inheritance using a discriminator column?
http://docs.jboss.org/hibernate/orm/4.1/manual/en-US/html_single/#inheritance-tablepersubclass-discriminator
 
Vinay Taneja
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Mac OS X IntelliJ IDE Spring Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic