Joins with daughter entities

Asked

Viewed 45 times

0

I have a parent entity called OCR, this has 2 that are specialization(inheritance): OCR_A, OCR_B.

My hql needs to contemplate the 2, IE, I need to give Join in the 2. I start like this:

select o from OCR o

How do I do join in the 2 child tables? Note: The ID of each is inherited from the OCR entity.

1 answer

0

Your search for the parent table will automatically bring the daughter tables regardless of your query.

Why? The framework (say Hibernate) does not know if you, after taking the parent entity, will want to do (or try) a cast for some of the daughter entities, so he already cautions of this situation.

If you ask to display the SQL of this query, you will see that the child tables will also be in the query through a LEFT JOIN. Something like that, if you’re using JOIN table for inheritance:

SELECT ocr.*, ocra.*,ocrb.* FROM OCR ocr
LEFT JOIN OCR_A ocra ON ocra.id = ocr.id
LEFT JOIN OCR_B ocrb ON ocrb.id = ocr.id

If you’re using strategy SINGLE table, all information is in the same table, then you will see in SQL only one table:

SELECT ocr.* FROM OCR ocr

Browser other questions tagged

You are not signed in. Login or sign up in order to post.