Search This Blog

Friday, 2 November 2012

Eager fetching does not work for parallel bags

In our previous posts we saw how Hibernate allows the use of eager fetching for collections. We also saw that with more than one collection having eager fetch enabled, how the Cartesian product problem was encountered. But this problem will never occur with Bags. Why? Because bags do not support this feature.
Consider the example of a Basket which has a bag of dry fruits and bag of exotic fruits.
public class FruitBag {
    private Integer id;
    private String color;
    private Collection<DryFruit> dryFruits = new LinkedList<DryFruit>();
    private Collection<ExoticFruit> exoticFruits = new LinkedList<ExoticFruit>();
The hbm for the same is as below:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
<hibernate-mapping package="com.collection.bag.join">
    <class name="FruitBag" table="FRUIT_BAG">
        <id name="id" type="integer">
            <column name="ID" />
            <generator class="native" />
        <property name="color" type="string">
            <column name="COLOR" length="50" not-null="true" />
        <bag name="dryFruits" cascade="all-delete-orphan" inverse="true"
            <key column="BAG_ID" not-null="true" />
            <one-to-many class="DryFruit" />
        <bag name="exoticFruits" cascade="all-delete-orphan" inverse="true"
            <key column="BAG_ID" not-null="true" />
            <one-to-many class="ExoticFruit" />

On trying to load a FruitBag object :
public static void testLoad() {
    Session session = sessionFactory.openSession();
    Transaction transaction = session.beginTransaction();
    FruitBag basket = (FruitBag) session.load(FruitBag.class, fruitBagId);
    System.out.println("Number of fruits in " + basket.getColor() + " is "
        + (basket.getDryFruits().size() + basket.getExoticFruits()
On running the code an exception is all we get
Exception in thread "main" org.hibernate.HibernateException: cannot simultaneously
fetch multiple bags
    at org.hibernate.loader.BasicLoader.postInstantiate(
    at org.hibernate.loader.entity.EntityLoader.<init>(
    at org.hibernate.loader.entity.EntityLoader.<init>(
    at org.hibernate.loader.entity.EntityLoader.<init>(
    at org.hibernate.loader.entity.BatchingEntityLoader.createBatchingEntityLoader(
    at org.hibernate.persister.entity.AbstractEntityPersister.createEntityLoader(Ab
    at org.hibernate.persister.entity.AbstractEntityPersister.createEntityLoader(Ab
    at org.hibernate.persister.entity.AbstractEntityPersister.createLoaders(Abstrac
    at org.hibernate.persister.entity.AbstractEntityPersister.postInstantiate(Abstr
    at org.hibernate.persister.entity.SingleTableEntityPersister.postInstantiate(Si
    at org.hibernate.impl.SessionFactoryImpl.<init>(
    at org.hibernate.cfg.Configuration.buildSessionFactory(
    at com.collection.bag.join.TestJoinBagProblem.main(
The reason is simple:
Bags allow duplicates. As we saw earlier, when eager fetching parallel collections a lot of duplicate data is generated. Knowing which of the duplicates are actually valid Bag Entities is difficult. Sub selects or fetch selects need to be used here.
Once one of the joins was removed, the code worked.I removed fetch="join" from dryFruits and the code worked fine.
/* load com.collection.bag.join.FruitBag */ 
        fruitbag0_.ID as ID0_1_,
        fruitbag0_.COLOR as COLOR0_1_,
        exoticfrui1_.BAG_ID as BAG3_3_,
        exoticfrui1_.ID as ID3_,
        exoticfrui1_.ID as ID2_0_,
        exoticfrui1_.Name as Name2_0_,
        exoticfrui1_.bag_id as bag3_2_0_ 
        FRUIT_BAG fruitbag0_ 
    left outer join
        EXOTIC_FRUIT exoticfrui1_ 
            on fruitbag0_.ID=exoticfrui1_.BAG_ID 
        fruitbag0_.ID= ?
    /* load one-to-many com.collection.bag.join.FruitBag.dryFruits */ 
        dryfruits0_.BAG_ID as BAG3_1_,
        dryfruits0_.ID as ID1_,
        dryfruits0_.ID as ID1_0_,
        dryfruits0_.Name as Name1_0_,
        dryfruits0_.bag_id as bag3_1_0_ 
        DRY_FRUIT dryfruits0_ 
        dryfruits0_.BAG_ID= ?
It is when we have more than one bag with join fetch, that the code fails. It in fact fails at start up itself. Hibernate doesn't even attempt to generate the queries.

1 comment: