-->
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: JPQL with inheritance - N+1 Select
PostPosted: Wed Jan 06, 2010 8:49 pm 
Newbie

Joined: Wed Jan 06, 2010 8:36 pm
Posts: 1
I have this inheriance tree
Code:
public class Entry {
   ...
   private Long id;
   private String field1;

   ...getters and setters...
}

public class InEntry extends Entry {
   ...
   private List<OutEntry> outEntries;

   ...getters and setters...
}

public class OutEntry extends Entry {
   ...
   private InEntry inEntry;

   ...getters and setters...
}


I have a JPQL that fetches a List<Entry> based on some criteria, the simplest form, i.e. no criteria at all, looks like:
Code:
from Entry e
where e.id in (
   select ie.id
   from InEntry ie
      left outer join ie.outEntries oe
   where oe.field1 is null
) or e.id in (
   select oe.id
   from InEntry ie
      left outer join ie.outEntries oe
   where oe.field1 is not null
)


Once I get this list, I am putting the data into a DTO, at which stage, if what i have is an instance of OutEntry, I call ((OutEntry)entry).getInEntry().

This results in an N+1 select. I want to optimize my code to avoid that N+1 select. I'm using Hibernate+JPA+Spring. I use JPA Annotations, but I'm willing to use Hibernate annotations if that can solve my problem.

All my joins are using a LAZY fetch.

Is there a way I can do this using annotations, or JPQL, or HQL?


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.