3

I have a 1-many relationship between class A and class B, represented by a foreign key relation between two tables in the database. and I want hibernate to eagerly load the collection of Bs so that it can be traversed outside a session.

So I specify lazy="false" on both the one-to-many and many-to-one mapping entry.

B.hbm :

<many-to-one cascade="all" fetch="join" lazy="false"
   class="A" name="...">
   <column name="adgroup_id"/>
  </many-to-one>

A.hbm

<list cascade="all" inverse="true" name="..." lazy="false" fetch="join">
      <key column="adgroup_id" />
       <one-to-many class="B"  />
</list>

I notice that the sql executed by hibernate indeed returns the expected number of rows, but when I call

A.getBs(), I get too many elements. Indeed, since my ids in the database are auto-assigned, it seems to return n+1 elements where n is the currently highest id in the table of Bs.

What is going on here ?

I am using the Spring hibernate template btw, calling template.get(class,id) to return the A

skaffman
  • 398,947
  • 96
  • 818
  • 769
ThuneGrill
  • 71
  • 1
  • 7

1 Answers1

-4

Very good description of n+1 problem: What is SELECT N+1?

Community
  • 1
  • 1
Nikunj
  • 3,100
  • 2
  • 20
  • 19
  • 6
    Sorry, I fail to see how your answer is useful. Only a single select is performed here, returning the expected number of rows. However, the number of entries in the returned collection in Java I get too many elements – ThuneGrill Apr 29 '11 at 12:28