Hi,
I have a problem of N+1 selection problem with the following class hierarchy:
Code:
@Entity
class Survey {
@OneToOne(cascade = CascadeType.ALL, optional = false)
private SurveyPayload payload;
}
@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
abstract class SurveyPayload {
}
@Entity
class ShortPayload extends SurveyPayload {
String question1;
}
@Entity
class LongPayload extends SurveyPayload {
String question1;
String question2;
String question3;
}
In the database I have Survey table, ShortPayload and LongPayload tables.
I currently have about 300 surveys all of type LongPayload.
When I execute the following JPQL (with Hibernate as provider) I see in the output the N+1 problem:
Code:
select s from Survey s join fetch s.payload
I see that Hibernate execute DB command for selecting all the surveys and join it with union of both ShortPayload and LongPayload tables; this select result has all the required information to create the objects, but then hibernate start to execute one select statement per result object.
I can't write "join fetch" select query because Short and Long payload have different properties.
How can I make Hibernate select everything in one shot instead of N+1 selects?
Thank you,
Ido.