I would suggest:
Organisation:
Code:
/**
* @hibernate.class
*/
class Organisation
{
private Long id;
private List addresses= new ArrayList();
/**
* @hibernate.id generator-class="native"
*/
public Long getId()
{
return id;
}
private void setId(Long id)
{
this.id = id;
}
/**
* @hibernate.bag lazy="false" inverse="true" cascade="all-delete-orphan"
* @hibernate.collection-key column="organisation"
* @hibernate.collection-one-to-many class="package.Addresses"
*/
List getAddresses()
{
return addresses;
}
private void setAddresses(List addresses)
{
this.addresses = addresses;
}
public void addAddress(Address address)
{
address.setOrganisation(this);
addresses.add(address);
}
}
Address:
Code:
/**
* @hibernate.class
*/
class Address
{
private Long id;
private Organisation organisation;
/**
* @hibernate.id generator-class="native"
*/
public Long getId()
{
return id;
}
private void setId(Long id)
{
this.id = id;
}
/**
* @hibernate.many-to-one not-null="true"
*/
public Organisation getOrganisation()
{
return organisation;
}
public void setOrganisation(Organisation organisation)
{
this.organisation = organisation;
}
}
Now You have to save Organisation only once. At this point, you can add Addresses without saving easily:
Code:
Session s = {init session}
Transaction tx = s.beginTransaction();
Organisation o = new Organisation();
s.save(o);
//are saved automatically
Address a1 = new Address();
o.addAddress(a1);
Address a2 = new Address();
o.addAddress(a2);
tx.commit();
s.close();
If You need the Mapping file ... just ask... Think, You are using xcdoclet;)