When you have a foreign key which references a nonprimary key you should use property-ref Attribute
Its documentation
The name of a property of the associated class that is joined to this foreign key
And because your Collection is immutable you should set up mutable attribute To false
Here goes your mapping
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--Set up your package right here-->
<hibernate-mapping package="br.com.ar">
<class name="Aa" table="A">
<id name="id">
<generator class="native"/>
</id>
</class>
<class name="Bb" table="B">
<id name="id">
<generator class="native"/>
</id>
<many-to-one name="aa" column="A_ID" class="Aa"/>
</class>
<class name="Cc" table="C">
<id name="id">
<generator class="native"/>
</id>
<many-to-one name="aa" column="A_ID" class="Aa"/>
<bag name="bbList" table="B" mutable="false">
<key column="A_ID" property-ref="aa"/>
<one-to-many class="Bb"/>
</bag>
</class>
</hibernate-mapping>
Each class is described as follows
br.com.ar.Aa
public class Aa {
private Integer id;
// getter's and setter's
}
br.com.ar.Bb
public class Bb {
private Integer id;
private Aa aa;
// getter's and setter's
}
br.com.ar.Cc
public class Cc {
private Integer id;
private Aa aa;
private Collection<Bb> bbList = new ArrayList<Bb>();
// getter's and setter's
}
ADDED AS WORKAROUND
Well, let's see first use case
query = new StringBuilder().append("SELECT ")
.append("{cc.*} ")
.append("from ")
.append("C cc ")
.append("where ")
.append("cc.id = 1 ")
.toString();
You said
The object is loaded with all data and its Bb related objects. On this object we can operate, change it and update the modifications in database
As you can see, your NATIVE SQL just retrieve Cc objects. No join. But you said it retrieves all of related objects, including The bbList. If so, it occurs because you have a mapping like (Notice fetch="select" lazy="false")
<class name="Cc" table="C">
...
<bag name="bbList" table="B" mutable="false" fetch="select" lazy="false">
<key column="A_ID" property-ref="aa"/>
<one-to-many class="Bb"/>
</bag>
</class>
fetch="select" uses an additional select. For one-to-many and many-to-many relationship (bbList, right ???) the fetch attribute works in conjunction with the lazy attribute to determine how and when the related collection are loaded. If lazy="true" then the collection is loaded when it is accessed by the application, but if lazy="false" then Hibernate loads the collection immediately using a separate SQL SELECT statement.
But if run
query = new StringBuilder().append("SELECT ")
.append("{cc.*} ")
.append("from ")
.append("C cc ")
.append("where ")
.append("cc.id in (1,2) ")
.toString();
I get The expected
org.hibernate.HibernateException: collection is not associated with any session
Why ???
You want To join Cc and its related bbList Through A_ID column which implies The use of property-ref attribute, right ???
<class name="Cc" table="C">
...
<bag name="bbList" table="B" mutable="false" fetch="select" lazy="false">
<key column="A_ID" property-ref="aa"/>
<one-to-many class="Bb"/>
</bag>
</class>
It happens if you want To use A_ID as primary-key, It must be unique. Because There are a lot of aa properties with The same value, you will get this exception. The first use case does not throw any exception because you have just retrieved one entity
Workaround
Try to get one by one
List<Cc> resultList = new ArrayList<Cc>();
for (Integer id : new Integer[] {1, 2}) {
query = new StringBuilder().append("SELECT ")
.append("{cc.*} ")
.append("from ")
.append("C cc ")
.append("where ")
.append("cc.id = :id ")
.toString();
resultList.addAll(
session.createSQLQuery(query)
.addEntity("cc", Cc.class)
.setParameter("id", id)
.list());
}
Or use plain JDBC queries