Anyone know why the many-to-many mappings below won't persist data into the association table (a2b)? All of the data gets persisted except the associtations, which is unfortunate since that's what I am most interested in. Nothing unusual in the logs and show_sql shows a query to 'a2b' but not an insert of any data.
Hibernate version:2.1.6
Mapping documents:
Alpha.hbm.xml
Code:
<hibernate-mapping package="com.rhoworld.tests.sharedcollection">
<class name="Alpha" table="alpha">
<id name="id" column="id" type="java.lang.Integer">
<generator class="native"/>
</id>
<set name="betas" inverse="false" table="a2b" lazy="true" cascade="all-delete-orphan">
<key column="alpha_id"/>
<many-to-many column="beta_id" class="Beta"/>
</set>
</class>
</hibernate-mapping>
Beta.hbm.xmlCode:
<hibernate-mapping package="com.rhoworld.tests.sharedcollection">
<class name="Beta" table="beta">
<id name="id" column="id" type="java.lang.Integer">
<generator class="native"/>
</id>
<set name="alphas" inverse="true" table="a2b" lazy="true">
<key column="beta_id"/>
<many-to-many column="alpha_id" class="Alpha"/>
</set>
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():Code:
Configuration config=new Configuration();
config.addClass(Alpha.class);
config.addClass(Beta.class);
SessionFactory factory=config.buildSessionFactory();
Session session=factory.openSession();
Alpha a1=new Alpha();
Alpha a2=new Alpha();
Beta b1=new Beta();
Beta b2=new Beta();
Beta b3=new Beta();
session.saveOrUpdate(b1);
session.saveOrUpdate(b2);
session.saveOrUpdate(b3);
session.saveOrUpdate(a1);
session.saveOrUpdate(a2);
Integer a1key=a1.getId();
Integer a2key=a2.getId();
Integer b1key=b1.getId();
Integer b2key=b2.getId();
Integer b3key=b3.getId();
a1=null;
a2=null;
b1=null;
b2=null;
b3=null;
System.gc();
session.disconnect();
session=factory.openSession();
a1=(Alpha)session.load(Alpha.class, a1key);
a2=(Alpha)session.load(Alpha.class, a2key);
b1=(Beta)session.load(Beta.class, b1key);
b2=(Beta)session.load(Beta.class, b2key);
b3=(Beta)session.load(Beta.class, b3key);
a1.addBeta(b1);
a1.addBeta(b2);
a1.addBeta(b3);
b1.addAlpha(a1);
b2.addAlpha(a1);
b3.addAlpha(a1);
assertTrue(b1.getAlphas().contains(a1)); //Pass
assertTrue(b2.getAlphas().contains(a1)); //Pass
assertTrue(b3.getAlphas().contains(a1)); //Pass
assertTrue(a1.getBetas().contains(b1)); //Pass
assertTrue(a1.getBetas().contains(b2)); //Pass
assertTrue(a1.getBetas().contains(b3)); //Pass
assertTrue(a2.getBetas().isEmpty()); //Pass
a1key=a1.getId();
a2key=a2.getId();
b1key=b1.getId();
b2key=b2.getId();
b3key=b3.getId();
a1=null;
a2=null;
b1=null;
b2=null;
b3=null;
System.gc();
session.disconnect();
session=factory.openSession();
a1=(Alpha)session.load(Alpha.class, a1key);
a2=(Alpha)session.load(Alpha.class, a2key);
b1=(Beta)session.load(Beta.class, b1key);
b2=(Beta)session.load(Beta.class, b2key);
b3=(Beta)session.load(Beta.class, b3key);
assertTrue(!b1.getAlphas().isEmpty()); //Fail.
assertTrue(!b2.getAlphas().isEmpty()); //Fail.
assertTrue(!b3.getAlphas().isEmpty()); //Fail.
assertTrue(!a1.getBetas().isEmpty()); //Fail.
assertTrue(a2.getBetas().isEmpty()); //Fail.
assertTrue(b1.getAlphas().contains(a1)); //Fail.
assertTrue(b2.getAlphas().contains(a1)); //Fail.
assertTrue(b3.getAlphas().contains(a1)); //Fail.
assertTrue(a1.getBetas().contains(b1)); //Fail.
assertTrue(a1.getBetas().contains(b2)); //Fail.
assertTrue(a1.getBetas().contains(b3)); //Fail.
Name and version of the database you are using: HSQL (Unknown Version. Assuming latest.)