-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Association of many collections to a single entity
PostPosted: Mon Apr 04, 2005 11:00 am 
Expert
Expert

Joined: Fri Nov 07, 2003 4:24 am
Posts: 315
Location: Cape Town, South Africa
Hibernate version: 2.1.8
Name and version of the database you are using: Postgres 8.0
Hi,

Is there any way to manually fetch a collection-type association and attach it to a set of entities so that you could get around the single collection fetching limitation?

For example:

I have an entity A which has two associations (Bee and Cee) both mapped as lazy collections (one-to-many from A)
Code:
(Bee) *---------1 (A) 1---------* (Cee)


Currently it is only possibly to perform a single fetch join so I do the following:
Code:
List aasWithBeesInitialised = session.createQuery(
    "from A ac "+
    "left join fetch a.bee b")
    .list();


List ceesWithAasInitialised = session.createQuery(
    "from Cee c "
     "left join fetch c.a")
    .list();


Now somehow (Yes - i know how to do this manually - but I'm looking for a transparent solution) attach the cees to a.

I suppose that I'm barking up the tree of 'transparent re-association' within the session scope. Any ideas?

TIA


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 04, 2005 12:08 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
change to
Code:
List aasWithBeesInitialised = session.createQuery(
    "from A ac "+
    "left join fetch a.bee b")
    .list();


List aasWithCeesInitialised = session.createQuery(
    "from A ac "+
    "left join fetch a.cee c")
    .list(); 


and all will be loaded, you can continue working with aasWithBeesInitialised or aasWithCeesInitialised

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 04, 2005 1:28 pm 
Expert
Expert

Joined: Fri Nov 07, 2003 4:24 am
Posts: 315
Location: Cape Town, South Africa
Thanks Anthony - works like a charm.

For anyone that is interested - here is a test case that proves it
Code:
package test;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import net.sf.hibernate.Hibernate;
import net.sf.hibernate.Session;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.Transaction;
import net.sf.hibernate.cfg.Configuration;
import junit.framework.TestCase;

public class ABCTest extends TestCase {

   
    public ABCTest() {
        super();
    }

    public ABCTest(String arg0) {
        super(arg0);
    }

    public void testIt() throws Exception {
        SessionFactory sf = new Configuration().configure().buildSessionFactory();
        insertData(sf);
        queryData(sf);
    }
   
    private void insertData(SessionFactory sf) throws Exception {
        Session session = sf.openSession();
        try {
            Transaction tx = session.beginTransaction();
           
            Aaa a = new Aaa();
            a.setName("a");
           
            Bee b1 = new Bee();
            b1.setName("b1");
           
            Bee b2 = new Bee();
            b2.setName("b2");
           
            List bees = new ArrayList();
            bees.add(b1);
            bees.add(b2);
            a.setBees(bees);
           
            Cee c1 = new Cee();
            c1.setName("c1");
           
            Cee c2 = new Cee();
            c2.setName("c2");
           
            List cees = new ArrayList();
            cees.add(c1);
            cees.add(c2);
            a.setCees(cees);
           
            session.save(a);
            tx.commit();
        }
        finally {
            session.close();
        }
    }
   
    private void queryData(SessionFactory sf) throws Exception {
        Session session = sf.openSession();
        try {
            session.createQuery(
                    "from Aaa a "+
                    "left join fetch a.bees b")
                    .list();           
           
            List aasWithCeesInitialised = session.createQuery(
                    "from Aaa a "+
                    "left join fetch a.cees c")
                    .list();
           
            for (Iterator iter = aasWithCeesInitialised.iterator(); iter.hasNext(); ) {
                Aaa a = (Aaa)iter.next();
                assertTrue(Hibernate.isInitialized(a));
                assertTrue(Hibernate.isInitialized(a.getBees()));
                assertTrue(Hibernate.isInitialized(a.getCees()));
            }
           
        }
        finally {
            session.close();
        }
    }
}


Code:
package test;

import java.util.Collection;
/**
* @hibernate.class
*      table = "aaa"
*/
public class Aaa {
   
    private Integer id;
    private String name;
   
    private Collection bees;
    private Collection cees;

    public Aaa() {
        super();
    }
   
    /**
     * @hibernate.id
     *      generator-class = "native"
     *      unsaved-value = "null"
     *      type = "int"
     */
    public Integer getId() { return id; }
    public void setId(Integer id) { this.id = id; }   
   
    /**
     * @hibernate.property
     *      not-null = "true"
     */
    public String getName() { return name; }
    public void setName(String name) { this.name = name; }
   
    /**
     * @hibernate.bag
     *      cascade = "all"
     *      lazy = "true"
     * @hibernate.collection-key
     *      column = "a_id"
     * @hibernate.collection-one-to-many
     *      class = "test.Bee"
     */
    public Collection getBees() { return bees; }
    public void setBees(Collection bees) { this.bees = bees; }
   
    /**
     * @hibernate.bag
     *      cascade = "all"
     *      lazy = "true"
     * @hibernate.collection-key
     *      column = "a_id"
     * @hibernate.collection-one-to-many
     *      class = "test.Cee"
     */
    public Collection getCees() { return cees; }
    public void setCees(Collection cees) { this.cees = cees; }   
}


Code:
package test;
/**
* @hibernate.class
*      table = "bee"
*/
public class Bee {
   
    private Integer id;
    private String name;

    public Bee() {
        super();
    }
   
    /**
     * @hibernate.id
     *      generator-class = "native"
     *      unsaved-value = "null"
     *      type = "int"
     */
    public Integer getId() { return id; }
    public void setId(Integer id) { this.id = id; }   
   
    /**
     * @hibernate.property
     *      not-null = "true"
     */
    public String getName() { return name; }
    public void setName(String name) { this.name = name; }
}

Code:
package test;
/**
* @hibernate.class
*      table = "cee"
*/
public class Cee {
   
    private Integer id;
    private String name;

    public Cee() {
        super();
    }
    /**
     * @hibernate.id
     *      generator-class = "native"
     *      unsaved-value = "null"
     *      type = "int"
     */
    public Integer getId() { return id; }
    public void setId(Integer id) { this.id = id; }   
   
    /**
     * @hibernate.property
     *      not-null = "true"
     */
    public String getName() { return name; }
    public void setName(String name) { this.name = name; }
}


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.