rodrigo.bmts wrote:
Hi,
When I delete or insert a Child from Collection and save the Main object, hibernate doesn't insert or delete row in the Child table, but when I just update an Object that already exists, this one is corrected updated in the table.
Just to be sure:
When you say 'update an Object that already exists', you mean changing children on an object fetched by Hibernate (and are you adding or only deleting children? - behavior may differ)
When you say 'save the Main', you mean a new Main object, with a new set of children, saved into the database?
Parhaps you forgot to set the Child backpointer to the main object when
adding a new child?
Code:
public class PersonVO extends AbstractVO {
...
public void setAddresses(Set<AddressVO> address) {
this.address.clear();
// Assure that Person backpointers are set prior to adding Address to our set
for (AddressV0 addr: address) {
addAddress(addr);
}
}
public void addAddresses(AddressVO addr) {
// Set the Person backpointer PRIOR to adding to the set
addr.setPerson(this);
this.address.add(addr);
}
}
Note that (not knowing about your implementation of equals(), hashCode()), I am assuming that hashCode may change when setting Person backpointer.
W/regards to mapping, I always try to be explicit:
cascade="save-update,delete,all-delete-orphan,evict"
Last but not least ... sets are nice, but with Hibernate, you must be carefull - using other collections is less risky (Set does not expect any member modifications - which may change hashCode() value).
Do not forget to rate!