Search code examples
hibernatecollectionsmapping

Hibernate Mapping problem with unrelated collection


Welcome,

I've some problem with Hibernate mapping.

Database structure:

TableA
 -ID_A --PK

TableB
 -ID_B --PK
 -ID_A -- FK -> TableA

TableC
 -ID_C -- PK
 -ID_A -- FK -> TableA

POJO structure:

class TableA extends Pojo {

 /*Some Fields*/

}

class TableB extends Pojo {

  TableA tableA; 

 /*Some properties*/

}

class TableC extends Pojo {

 TableA tableA;

 Collection<tableB> tableBs;

}

What i want to have is the collection of TableB elements in mapping for a TableC Pojo, the mapping key is the tableA.

This collection should be read only.

The mapping should be hbm not annotations.

I have probably done this for every possible way... the closes i get is case that when i operate on one TableC object then is everything correct but if i load the collection of them then only the last one have proper collection set.

UPDATE: Case description.

The use case 1: Loading single object of TableC

Session session = (Session) getHibernateTemplate().getSessionFactory().openSession();
SQLQuery sqlQuery = session.createSQLQuery("SELECT c.* FROM TableC c WHERE c.ID_C = 1"); //Oracle
  sqlQuery.addEntity("c", TableC.class);
return sqlQuery.list(); //Return list with sigle object of TableC

In this case everything works fine as should. The object is loaded with all data and proper items in list of TableB objects. On this object we can operate, change it and update the modifications in database.

The use case 2 Loading collections of objects

Session session = (Session) getHibernateTemplate().getSessionFactory().openSession();
SQLQuery sqlQuery = session.createSQLQuery("SELECT c.* FROM TableC c WHERE c.ID_C in (1,2)"); //Oracle
  sqlQuery.addEntity("c", TableC.class);
return sqlQuery.list(); // throws "collection is not associated with any session" 

In this case Hibernate throw an exception while retrieving next object.

*the codes are only samples, the session is closed after all.


Solution

  • After some research, the problem is in Hibernate. It is known as bug HHH-2862

    It is basically caused by having an eager collection where the key of the collection is not unique in the results.

    When Hibernate initialize collection using 'persistenceContext.addUninitializedCollection()' and this will detect that the collection with the given key has already been added, then sets old instance to null and current to the collection. However, that collection has already been added to the persistence context by an earlier call, and when StatefulPersistenceContext.initializeNonLazyCollections() iterate over all of the collections in the persistent context calling forceInitialization() on the references hit the null reference which throws a "collection is not associated with any session" exception". And this explains why in my case only the last object has the reference and with only one everything was working fine, and yours assumptions regarding the lazy initialization problem.

    Some thoughts how to bypass this bug ?