-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: N+1 Selection
PostPosted: Tue Apr 13, 2010 6:57 am 
Beginner
Beginner

Joined: Thu Jul 31, 2008 8:28 pm
Posts: 24
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.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.