Right, I've taken the mappings you've had trouble with (title->titleParcelIntersection->parcel) and created a minimal stand alone test (no data fields, just mappings).
I've managed to save a title with 2 parcels and load it back succesfully. Code and mappings included below. Hopefully it will give you some insight into where you might be going wrong.
HibernateUtil.java
Code:
package test.jonnyo;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
private static SessionFactory factory;
static {
Configuration config = new Configuration()
.addClass(Title.class)
// HSQLDB Config
.setProperty("hibernate.dialect", "org.hibernate.dialect.HSQLDialect")
.setProperty("hibernate.connection.driver_class", "org.hsqldb.jdbcDriver")
.setProperty("hibernate.connection.url", "jdbc:hsqldb:mem:test")
.setProperty("hibernate.connection.username", "sa")
.setProperty("hibernate.connection.password", "")
.setProperty("hibernate.hbm2ddl.auto", "create-drop")
// MYSQL Config
// .setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect")
// .setProperty("hibernate.connection.driver_class", "com.mysql.jdbc.Driver")
// .setProperty("hibernate.connection.url", "jdbc:mysql://localhost/test")
// .setProperty("hibernate.connection.username", "user")
// .setProperty("hibernate.connection.password", "password")
// .setProperty("hibernate.hbm2ddl.auto", "create-drop")
.setProperty("hibernate.cache.provider_class", "org.hibernate.cache.NoCacheProvider")
.setProperty("hibernate.show_sql", "true");
HibernateUtil.setSessionFactory(config.buildSessionFactory());
}
public static synchronized Session getSession() {
if (factory == null) {
factory = new Configuration().configure().buildSessionFactory();
}
return factory.openSession();
}
public static void setSessionFactory(SessionFactory factory) {
HibernateUtil.factory = factory;
}
}
TestIt.java
Code:
package test.jonnyo;
import org.hibernate.Session;
public class TestIt {
public static void main(String[] args) {
TitleId titleId = createTestData();
Session s = HibernateUtil.getSession();
s.beginTransaction();
Title title = (Title)s.load(Title.class, titleId);
System.out.println("TitleParcelIntersections: "+title.getTitleParcelIntersections().size());
s.getTransaction().commit();
s.close();
}
private static TitleId createTestData() {
Session s = HibernateUtil.getSession();
s.beginTransaction();
System.out.println("creating test data");
TitleId titleId = new TitleId("AD", "12345AB");
Title title = new Title();
title.setId(titleId);
s.save(title);
Parcel parcel1 = new Parcel();
s.save(parcel1);
Parcel parcel2 = new Parcel();
s.save(parcel2);
TitleParcelIntersectionId titleParcelIntersectionId1 = new TitleParcelIntersectionId(title, parcel1);
TitleParcelIntersection titleParcelIntersection1 = new TitleParcelIntersection();
titleParcelIntersection1.setId(titleParcelIntersectionId1);
s.save(titleParcelIntersection1);
TitleParcelIntersectionId titleParcelIntersectionId2 = new TitleParcelIntersectionId(title, parcel2);
TitleParcelIntersection titleParcelIntersection2 = new TitleParcelIntersection();
titleParcelIntersection2.setId(titleParcelIntersectionId2);
s.save(titleParcelIntersection2);
s.getTransaction().commit();
s.close();
System.out.println("test data created");
return titleId;
}
}
Title.hbm.xml (contains mappings for all classes)
Code:
<?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">
<hibernate-mapping package="test.jonnyo">
<class name="Title" table="title" >
<composite-id name="id" class="TitleId">
<key-property name="ltbDistrictCd" type="string">
<column name="LTB_DISTRICT_CD" length="2"/>
</key-property>
<key-property name="titleNmbr" type="string">
<column name="TITLE_NMBR" length="11"/>
</key-property>
</composite-id>
<set name="titleParcelIntersections" inverse="true">
<key>
<column name="LTB_DISTRICT_CD" not-null="true"/>
<column name="TITLE_NMBR" not-null="true"/>
</key>
<one-to-many class="TitleParcelIntersection" />
</set>
</class>
<class name="TitleParcelIntersection" table="titleParcelIntersection">
<composite-id name="id" class="TitleParcelIntersectionId">
<key-many-to-one name="title" class="Title">
<column name="LTB_DISTRICT_CD"/>
<column name="TITLE_NMBR"/>
</key-many-to-one>
<key-many-to-one name="parcel" class="Parcel">
<column name="PRMNNT_PRCL_ID" length="4"/>
</key-many-to-one>
</composite-id>
</class>
<class name="Parcel" table="parcel">
<id name="prmnntPrclId" column="PRMNNT_PRCL_ID" type="integer" length="4" >
<generator class="native"/>
</id>
</class>
</hibernate-mapping>
Title.java
Code:
package test.jonnyo;
import java.util.Set;
public class Title {
private TitleId id;
private Set<TitleParcelIntersection> titleParcelIntersections;
public TitleId getId() {
return id;
}
public void setId(TitleId id) {
this.id = id;
}
public Set<TitleParcelIntersection> getTitleParcelIntersections() {
return titleParcelIntersections;
}
public void setTitleParcelIntersections(Set<TitleParcelIntersection> titleParcelIntersections) {
this.titleParcelIntersections = titleParcelIntersections;
}
}
TitleParcelIntersection.java
Code:
package test.jonnyo;
public class TitleParcelIntersection {
private TitleParcelIntersectionId id;
public TitleParcelIntersectionId getId() {
return id;
}
public void setId(TitleParcelIntersectionId id) {
this.id = id;
}
}
Parcel.java
Code:
package test.jonnyo;
public class Parcel {
private int prmnntPrclId;
public int getPrmnntPrclId() {
return prmnntPrclId;
}
public void setPrmnntPrclId(int prmnntPrclId) {
this.prmnntPrclId = prmnntPrclId;
}
}
TitleId.java
Code:
package test.jonnyo;
import java.io.Serializable;
public class TitleId implements Serializable {
private String ltbDistrictCd;
private String titleNmbr;
protected TitleId() {
}
public TitleId(String ltbDistrictCd, String titleNmbr) {
this.ltbDistrictCd = ltbDistrictCd;
this.titleNmbr = titleNmbr;
}
public String getLtbDistrictCd() {
return ltbDistrictCd;
}
public void setLtbDistrictCd(String ltbDistrictCd) {
this.ltbDistrictCd = ltbDistrictCd;
}
public String getTitleNmbr() {
return titleNmbr;
}
public void setTitleNmbr(String titleNmbr) {
this.titleNmbr = titleNmbr;
}
@Override
public boolean equals(Object obj) {
if (obj == null || !(obj instanceof TitleId)) {
return false;
}
TitleId other = (TitleId)obj;
return (other.ltbDistrictCd.equals(this.ltbDistrictCd) &&
other.titleNmbr.equals(this.titleNmbr));
}
@Override
public int hashCode() {
return ltbDistrictCd.hashCode() + titleNmbr.hashCode();
}
}
TitleParcelIntersectionId.java
Code:
package test.jonnyo;
import java.io.Serializable;
public class TitleParcelIntersectionId implements Serializable {
private Title title;
private Parcel parcel;
public TitleParcelIntersectionId() {
}
public TitleParcelIntersectionId(Title title, Parcel parcel) {
this.title = title;
this.parcel = parcel;
}
public Title getTitle() {
return title;
}
public void setTitle(Title title) {
this.title = title;
}
public Parcel getParcel() {
return parcel;
}
public void setParcel(Parcel parcel) {
this.parcel = parcel;
}
@Override
public boolean equals(Object obj) {
if (obj == null || !(obj instanceof TitleParcelIntersectionId)) {
return false;
}
TitleParcelIntersectionId other = (TitleParcelIntersectionId)obj;
return (other.title.equals(this.title) &&
other.parcel.equals(this.parcel));
}
@Override
public int hashCode() {
return title.hashCode() + parcel.hashCode();
}
}