问题
I have the following OneToMany relationships between Person and PhoneNumber:
class Person
{
@OneToMany(mappedBy = "person", cascade={CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REMOVE})
@Cascade({org.hibernate.annotations.CascadeType.DELETE_ORPHAN,org.hibernate.annotations.CascadeType.SAVE_UPDATE})
private Set<PhoneNumber> phoneNumbers = new HashSet<PhoneNumber>();
//.... omitted ....
}
class PhoneNumber
{
@ManyToOne(optional = false)
private Person person;
//... omitted ...
}
If I set the phone-number(s) during the initial creation of Person, those objects get persisted as expected. HOWEVER, if later on, I tried to update these phone-numbers, and do save on the Person object, only the Person object gets saved. The Phone-number objects do not.
Any idea what I did wrong? I've tried various ways, but none seems to work.
来源:https://stackoverflow.com/questions/22211887/hibernate-one-to-many-mapping-doesnt-saved-upon-update