Hi,
I'm developing an J2EE 6 application using JPA and Hibernate as provider.
I have entity called Survey which have property called payload of type SurveyPayload which map as OneToOne, that is for each survey I have exactly one payload and vice-versa. The payload property is required (not optional).
The SurveyPayload class is super class for two other classes (entities) which are called LongSurveyPayload and ShortSurveyPayload each contain different properties.
I've chose this design to be able to use surveys generally without caring which actual payload is attached to them.
The problem I'm facing is the usual N+1 select problem.
When I select all the surveys and I know in advance that I'm going to need the payload of each one I'm selecting them with the following query:
Code:
select s
from Survey s
join fetch s.surveyPayload
The output I see generated by hibernate is
Code:
INFO: Hibernate: select survey0_.id as id20_0_, ... surveypayl1_.clazz_ as clazz_1_ from Survey survey0_
inner join (
select id, .. 1 as clazz_ from ShortSurveyPayload
union
select id, .. 2 as clazz_ from LongSurveyPayload
) surveypayl1_ on survey0_.surveyPayload_id=surveypayl1_.id
I've trim the sql to make it clearerwhich really select all the surveys and join on a union of both payload types. But then I start to see a line for each survey to select its payload.
It is literally taking for ever to complete this select and there is only around 200 surveys and payloads.
Any ideas how to fix this problem?
Thank you,
Ido.