Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="learn.hibernate">
<class name="House" table="house">
<id name="id" column="id" />
<property name="address" column="address"/>
<set name="persons" lazy="true" inverse="true" cascade="all-delete-orphan">
<key column="id"/>
<one-to-many class="Person"/>
</set>
</class>
<class name="Person" table="person">
<id name="id" column="id" />
<property name="name" column="name"/>
<property name="houseId" column="house_id"/>
<many-to-one name="houseObject" class="House" column="house_id" insert="false" update="false"/>
</class>
</hibernate-mapping>
Code:
Session session = getSession( ... );
Transaction tx = session.beginTransaction();
tx.begin();
Person person1 = createPerson( ... );
Person person2 = createPerson( ... );
House house = createHouse( ... );
house.getPersons().add( person1 );
house.getPersons().add( person2 );
person1.setHouseObject( house );
person2.setHouseObject( house );
session.save( house );
tx.commit();