Hibernate 3.0.5, Sybase 12.5 database
Right, we have attached below an example database structure, mapping and java files. The structure is parent contains middle men and middle men contain children. What we want to do is add a collection to parent that when dereferenced will contain all the children contained by all the middle men. The code makes it clearer. When we run this test case for example:
Code:
public void testCanInsertAndRead() throws SQLException {
Parent p = new Parent();
MiddleMan m1 = new MiddleMan(p);
MiddleMan m2 = new MiddleMan(p);
Child c11 = new Child(m1);
Child c12 = new Child(m1);
Child c21 = new Child(m2);
Child c22 = new Child(m2);
session.save(p);
session.save(m1);
session.save(m2);
session.save(c22);
session.save(c21);
session.save(c12);
session.save(c11);
session.connection().commit();
}
We could like p.allChildren to contain c11, c12, c21, and c22 - in other words as if this sql was executed:
select c.*
from parent p
join middle_man m on m.parent_id = p.id
join child c on c.middle_man_id = m.id
where p.id = 1
We have tried to build the many-to-many relationship using this sql in a formula but did not solve it.
Can somebody please explain how to do this?
Code etc below.
Thanks very much,
Mike
Code:
-------------- Parent.java --------------------------
package mapping;
import java.util.Set;
public class Parent {
private Long id;
private Set allChildren;
}
-------------- end Parent.java --------------------------
-------------- MiddleMan.java --------------------------
package mapping;
public class MiddleMan {
private Long id;
private Parent parent;
public MiddleMan(Parent parent) {
this.parent = parent;
}
}
-------------- end MiddleMan.java --------------------------
-------------- Child.java --------------------------
package mapping;
public class Child {
private Long id;
private MiddleMan middleMan;
public Child(MiddleMan middleMan) {
this.middleMan = middleMan;
}
}
-------------- end Child.java --------------------------
-------------- Parent.hbm.xml --------------------------
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping default-access="field">
<class name="mapping.Parent" table="parent">
<id name="id" column="id" type="long">
<generator class="identity"/>
</id>
</class>
</hibernate-mapping>
-------------- end Parent.hbm.xml --------------------------
-------------- MiddleMan.hbm.xml --------------------------
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping default-access="field">
<class name="mapping.MiddleMan" table="middle_man">
<id name="id" column="id" type="long">
<generator class="identity"/>
</id>
<many-to-one name="parent" column="parent_id"/>
</class>
</hibernate-mapping>
-------------- end MiddleMan.hbm.xml --------------------------
-------------- Child.hbm.xml --------------------------
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping default-access="field">
<class name="mapping.Child" table="child">
<id name="id" column="id" type="long">
<generator class="identity"/>
</id>
<many-to-one name="middleMan" column="middle_man_id"/>
</class>
</hibernate-mapping>
-------------- end Child.hbm.xml --------------------------
-------------- SQL code --------------------------
drop table parent
create table parent (
id NUMERIC(10) IDENTITY
)
drop table middle_man
create table middle_man(
id NUMERIC(10) IDENTITY,
parent_id NUMERIC(10) NOT NULL
)
drop table child
create table child (
id NUMERIC(10) IDENTITY,
middle_man_id NUMERIC(10) NOT NULL
)
-------------- end SQL code --------------------------