Hello ualex, when mapping a collection with hibernate you have to think about two thinks
cascade and
inverse
I just gonna explain the inverse because it's a little confusing,
the inverse it's when the cascade will be triggered. "When" meaning that
you could do "programatically" write
Code:
Person p=new Person();
Address a=new Address();
//Set the values for p and a here
p.getAddress().add(a);
a.setPerson(p);
Hibernate must know what will be the trigger than activates the cascade
when you say
p.getAddres().add(a) or when you say
a.setPerson(p). If you set inverse to true then hibernate will ignore the
a.setPerson(p) trigger and it will only gonna persist "a". The trigger to Create the association between "a" and "p" will be
p.getAddress().add(a).
I suggest you to add a method to the Person class, something like this
Code:
public void addAddress(Address address)
{
getAddress().add(address);
address.setPerson(this);
}
and add to your mapping files, this
Code:
<set name="address" inverse="true" cascade="all">
<key column="personId" not-null="true" />
<one-to-many class="tutorial.Address" />
</set>
Regards,
I hope this help you, and it does please rate the post.