jonua
jonua

Reputation: 2005

Hibernate: how to create a relation with two tables?

I have a three tables:

table

-----------
|id       |
-----------
|data_type|
-----------
|data_id  |
-----------

table_data_a

----------
|id      |
----------
|table_id|
----------

table_data_b

----------
|id      |
----------
|table_id|
----------

data_type is a enum('data_a', 'data_b');

data_id is a relation to id fields of tables table_data_a or table_data_b

  1. It is correct approach to creating of the relations?
  2. How to describe this relations in Hibernate?

Upvotes: 0

Views: 81

Answers (1)

Narendra Pandey
Narendra Pandey

Reputation: 436

In my opinion -

Ans1) There is no need to make relationship between these tables(association or inheritance) as data_id can act as id of both the table(table_data_a and table_data_b).Its better to take it as String or int as per your datatype in db , and make separate call to both tables for search.

Ans2) Mapping through Association refernce

Mapping through inheritance refernce

Upvotes: 1

Related Questions