[hibernate-issues] [Hibernate-JIRA] Created: (HHH-5165) FetchMode=Eager not respected, N+1 SELECT Problem

David Bernhard (JIRA) noreply at atlassian.com
Tue Apr 27 07:46:59 EDT 2010


FetchMode=Eager not respected, N+1 SELECT Problem
-------------------------------------------------

                 Key: HHH-5165
                 URL: http://opensource.atlassian.com/projects/hibernate/browse/HHH-5165
             Project: Hibernate Core
          Issue Type: Bug
          Components: core
    Affects Versions: 3.3.2
         Environment: (as Maven artifacts)
org.hibernate:hibernate-core:jar:3.3.2.GA
org.hibernate:hibernate-annotations:jar:3.4.0.GA
org.hibernate:ejb3-persistence:jar:1.0.2.GA
org.hibernate:hibernate-validator:jar:3.1.0.GA
org.hibernate:hibernate-commons-annotations:jar:3.1.0.GA
org.hibernate:hibernate-search:jar:3.1.0.GA
org.hibernate:hibernate-entitymanager:jar:3.4.0.GA

            Reporter: David Bernhard
            Priority: Minor


Two classes A, B with 1:n relationship. Fetching all instances of B with an empty DetachedCriteria instance does not repsect FetchMode.JOIN but issues a SELECT for each A (N+1 SELECT problem).

This causes the bug:

findByCriteria(DetachedCriteria.forClass(B.class));

This works - but I need that second setFetchMode:

findByCriteria(DetachedCriteria.forClass(B.class)
  .setFetchMode("a", FetchMode.JOIN)
  .setFetchMode("a.bs", FetchMode.JOIN)
);

HQL works too. Here are two classes that cause the bug:

@Entity @Table(name = "A")
@SequenceGenerator(name = "keyid_generator", sequenceName = "A_sequence")
public class A extends AbstractIntKeyIntOptimisticLockingDto {
   private String name;
   private Set<B> bs;

   @OneToMany(mappedBy = "a", fetch = FetchType.EAGER)
   @Fetch(FetchMode.JOIN)
   public Set<B> getBs() {
      return bs;
   }
   public void setBs(Set<B> bs) {
      this.bs = bs;
   }

   public String getName() {
      return name;
   }
   public void setName(String name) {
      this.name = name;
   }
}

@Entity @Table(name = "B")
@SequenceGenerator(name = "keyid_generator", sequenceName = "B_sequence")
public class B extends AbstractIntKeyIntOptimisticLockingDto {

   private String name;
   private A a;

   @ManyToOne(fetch = FetchType.EAGER)
   @JoinColumn(name = "a", nullable = false,
   unique = false, updatable = false)
   @Fetch(FetchMode.JOIN)
   public A getA() {
      return a;
   }
   public void setA(A a) {
      this.a = a;
   }

   public String getName() {
      return name;
   }
   public void setName(String name) {
      this.name = name;
   }
}

-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators: http://opensource.atlassian.com/projects/hibernate/secure/Administrators.jspa
-
For more information on JIRA, see: http://www.atlassian.com/software/jira

        


More information about the hibernate-issues mailing list