Sorry, I was in a hurry the other night and was hoping to get away w/ being lazy. :-)
Your testcase is almost complete...it's just missing an additional one-to-many entity set from the base table (included in the LEFT JOIN FETCH).
Below is a full description that reproduces the failure.
SQL
Code:
create table mdlandon_zones_container
(
ZoneID integer not null default autoincrement,
ZoneName varchar(64) not null,
primary key (ZoneID)
);
create table mdlandon_approval_zone
(
ZoneID integer not null,
ApprovalID integer not null,
primary key (ZoneID, ApprovalID)
);
alter table mdlandon_approval_zone
add foreign key FK_1 (ZoneID)
references mdlandon_zones_container (ZoneID)
on update restrict
on delete restrict;
create table mdlandon_other_container
(
OtherID integer not null default autoincrement,
ZoneID integer not null,
OtherName varchar(64) not null,
primary key (OtherID)
);
alter table mdlandon_other_container
add foreign key FK_2 (ZoneID)
references mdlandon_zones_container (ZoneID)
on update restrict
on delete restrict;
insert into mdlandon_zones_container(ZoneID, ZoneName) values (1, 'testing');
insert into mdlandon_zones_container(ZoneID, ZoneName) values (2, 'testing2');
insert into mdlandon_approval_zone(ZoneID, ApprovalID) values (1, 33);
insert into mdlandon_approval_zone(ZoneID, ApprovalID) values (1, 34);
insert into mdlandon_approval_zone(ZoneID, ApprovalID) values (1, 35);
insert into mdlandon_approval_zone(ZoneID, ApprovalID) values (2, 133);
insert into mdlandon_approval_zone(ZoneID, ApprovalID) values (2, 234);
insert into mdlandon_approval_zone(ZoneID, ApprovalID) values (2, 335);
insert into mdlandon_other_container(OtherID, ZoneID, OtherName) values (1, 1, 'other1-1');
insert into mdlandon_other_container(OtherID, ZoneID, OtherName) values (2, 1, 'other1-2');
insert into mdlandon_other_container(OtherID, ZoneID, OtherName) values (3, 1, 'other1-3');
insert into mdlandon_other_container(OtherID, ZoneID, OtherName) values (4, 2, 'other2-1');
insert into mdlandon_other_container(OtherID, ZoneID, OtherName) values (5, 2, 'other2-2');
insert into mdlandon_other_container(OtherID, ZoneID, OtherName) values (6, 2, 'other2-3');
ZonesContainer.javaCode:
package test.mdlandon;
import java.util.Set;
public class ZonesContainer {
private Long zoneId;
private String name;
private Set zones;
private Set others;
public Long getZoneId() {
return zoneId;
}
public void setZoneId(Long zoneId) {
this.zoneId = zoneId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set getZones() {
return zones;
}
public void setZones(Set zones) {
this.zones = zones;
}
public Set getOthers() {
return others;
}
public void setOthers(Set others) {
this.others = others;
}
}
ZonesContainer.hbm.xmlCode:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping package="test.mdlandon">
<class name="ZonesContainer" table="mdlandon_zones_container">
<id name="zoneId" column="ZoneID">
<generator class="native" />
</id>
<property name="name" type="string">
<column name="ZoneName" length="64" not-null="true" />
</property>
<set name="zones" table="mdlandon_approval_zone">
<key column="ApprovalID" />
<element column="ZoneID" type="int"/>
</set>
<set name="others" inverse="true" cascade="save-update" fetch="subselect">
<key>
<column name="OtherID" not-null="true" />
</key>
<one-to-many class="test.mdlandon.OtherContainer" />
</set>
</class>
</hibernate-mapping>
OtherContainer.javaCode:
package test.mdlandon;
public class OtherContainer {
private Long otherId;
private String name;
private ZonesContainer zone;
public Long getOtherId() {
return otherId;
}
public void setOtherId(Long otherId) {
this.otherId = otherId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public ZonesContainer getZone() {
return zone;
}
public void setZone(ZonesContainer zone) {
this.zone = zone;
}
}
OtherContainer.hbm.xmlCode:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping package="test.mdlandon">
<class name="OtherContainer" table="mdlandon_other_container">
<id name="otherId" column="OtherID">
<generator class="native" />
</id>
<many-to-one name="zone" class="test.mdlandon.ZonesContainer">
<column name="ZoneID" />
</many-to-one>
<property name="name" type="string">
<column name="OtherName" length="64" not-null="true" />
</property>
</class>
</hibernate-mapping>
TestIt.javaCode:
package test.mdlandon;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.hibernate.Query;
import org.hibernate.Session;
public class TestIt {
public void testIt() throws Exception {
Session s = HibernateUtil.getSession();
Query q = s.createQuery("from ZonesContainer as zc left join fetch zc.others left join fetch zc.zones");
List result = q.list();
System.out.println("result size="+result.size());
ZonesContainer zc = (ZonesContainer)result.get(0);
System.out.println(""+zc.getZones());
System.out.println(""+zc.getOthers());
s.close();
}
public static void main (String args[]) throws Exception {
TestIt it = new TestIt();
it.testIt();
}
}
Console SQLCode:
Hibernate: select zonesconta0_.ZoneID as ZoneID0_0_, others1_.OtherID as OtherID2_1_, zonesconta0_.ZoneName as ZoneName0_0_, zones2_.ApprovalID as ApprovalID1__, zones2_.ZoneID as ZoneID1__, others1_.ZoneID as ZoneID2_1_, others1_.OtherName as OtherName2_1_, others1_.OtherID as OtherID0__, zones2_.ApprovalID as ApprovalID1__, zones2_.ZoneID as ZoneID1__ from mdlandon_zones_container zonesconta0_ left outer join mdlandon_other_container others1_ on zonesconta0_.ZoneID=others1_.OtherID left outer join mdlandon_approval_zone zones2_ on zonesconta0_.ZoneID=zones2_.ApprovalID
Console ErrorCode:
SEVERE: ASA Error -830: Alias 'ApprovalID1__' is not unique
org.hibernate.exception.SQLGrammarException: could not execute query
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:67)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.loader.Loader.doList(Loader.java:2223)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2104)
at org.hibernate.loader.Loader.list(Loader.java:2099)
at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:378)
at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:338)
at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:172)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1121)
at org.hibernate.impl.QueryImpl.list(QueryImpl.java:79)
at test.mdlandon.TestIt.testIt(Unknown Source)
at test.mdlandon.TestIt.main(Unknown Source)