title: Hibernate only loads one object of query-collection
body: <p>A few days ago I've encountered a strange behaviour in Hibernate and can't say why it appeared out of the sudden.</p>
  
  <p>There are two tables sharing an unidirectional 1:n relation. There is the record-catalog which contains <em>n</em> records. Under conditions I am not able to spot only one record is loaded. No error-messages occurred. No warnings either.</p>
  
  <p>Actually it's pretty easy stuff, always used to work. (Code for the objects in the end of this posting)</p>
  
  <p>Till now I can only say if the Record-Catalog has the ID 78 only one record will be loaded. For the other IDs it appears to be fine.</p>
  
  <p>I'm fetching my RC-Object like that:</p>
  
  <pre><code>public String getCatalog(){
  Session session = null;
  
      try {
  
          SessionFactory factory = HibernateUtil.getSessionFactory();
          session = factory.openSession();
          Transaction tx = session.beginTransaction();
          String hql = "SELECT R FROM rcatalog.Rcatalog as R WHERE rcatalogID_PK = " + rcid;
          Query query = session.createQuery(hql);
          return getOutputForPage(query.list().get(0)); //If I get the size of the records it's 1 here
  
      } catch (HibernateException | java.lang.IndexOutOfBoundsException hibex) {
          Logger.getLogger(BasisHibernate.class.getName()).log(Level.INFO, "Rcid: {0}", rcid);
          Logger.getLogger(BasisHibernate.class.getName()).log(Level.SEVERE, null, hibex);
      } finally {
          try {
              if (session != null) {
                  session.close();
              }
          } catch (HibernateException | NullPointerException hibex) {
          }
      }
      return "";
  }
  </code></pre>
  
  <p>So far this seems alright for me. The ID is the correct one. I decided to switch the sql-output on and get the following line 
  (M_ID --> ID for the Catalog
  D_ID --> ID for the Record
  ):</p>
  
  <blockquote>
  <pre><code>Hibernate: select myrecord0_.M_ID as M65_9_1_, myrecord0_.D_ID as D1_1_, myrecord0_.D_ID as D1_7_0_ from [MyProj].[dbo].[MyRecord] myrecord0_ where myrecord0_.M_ID=?
  </code></pre>
  </blockquote>
  
  <p>By using the query in the SQL Server Management Studio I get the amount of records listed as expected.</p>
  
  <p>Till now I have tried to delete the single record which appears but the behaviour keeps the same.</p>
  
  <p>I did some research already but haven't been too successful:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/8260064/hibernate-only-loads-one-object-from-a-list">Hibernate only loads one object from a list</a></li>
  <li><ul>
  <li>Entirely different error</li>
  </ul></li>
  <li><p><a href="http://www.coderanch.com/t/442282/ORM/databases/Hibernate-loads-element-collection" rel="nofollow">http://www.coderanch.com/t/442282/ORM/databases/Hibernate-loads-element-collection</a></p></li>
  <li><ul>
  <li>I don't think we have the same problem</li>
  </ul></li>
  <li><p><a href="https://stackoverflow.com/questions/24121020/hibernate-manytomany-only-returns-one-element">Hibernate manyToMany only returns one element</a></p></li>
  <li><ul>
  <li>No restrictions used</li>
  </ul></li>
  <li><p><a href="http://www.laliluna.de/jpa-hibernate-guide/ch02s02.html" rel="nofollow">http://www.laliluna.de/jpa-hibernate-guide/ch02s02.html</a></p></li>
  <li><ul>
  <li>Interesting article but I have tried switching lazy to eager, was restarting my servers, rebuilding the project. Still the same</li>
  </ul></li>
  </ul>
  
  <p>Here are my classes (As stated before, I'm not using FetchType.EAGER. This was for test-purposes only)</p>
  
  <pre><code>@Entity
  @Table(name = "[MyProj].[dbo].[Rcatalog]")
  public class Rcatalog implements Serializable {
  
  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  @Column(name = "M_ID")
  private Integer RcatalogID_PK;
  
  @ManyToOne()
  @JoinColumn(name = "W_ID", insertable = false, updatable = false)
  private UpperElement upperelement;
  
  @Column(name = "W_ID")
  @JoinColumn(name = "upperelementID_FK", referencedColumnName = "upperelementID_PK")
  private Integer upperelementID_FK;
  
  @OneToMany(mappedBy = "myrecordID_FK", fetch = FetchType.EAGER)
  @Sort(type = SortType.COMPARATOR, comparator = MyRecordComparator.class)
  private SortedSet&lt;MyRecord&gt; myrecord = new TreeSet&lt;&gt;();
  
  //Constructor, Getter, Setter...
  }
  </code></pre>
  
  <p>And there are the records:</p>
  
  <pre><code>@Entity
  @Table(name = "[MyProj].[dbo].[MyRecord]")
  public class MyRecord implements Serializable, PartElementInterface {
  
  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  @Column(name = "D_ID")
  private Integer myrecordID_PK;
  
  @Column(name = "M_ID")
  @JoinColumn(name = "myrecordID_FK", referencedColumnName = "myrecordID_PK")
  private Integer myrecordID_FK;
  
  @ManyToOne(fetch = FetchType.LAZY)
  @JoinColumn(name = "M_ID", insertable = false, updatable = false, nullable = true)
  private Rcatalog rcatalog;
  
  //Constructor, Getter, Setter and some more stuff
  }
  </code></pre>
  
  <p>My Hibernate-core version is 3.6.10.Final</p>