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; }
}