Hibernate: One-To-Many mapping doesn't saved upon update

烈酒焚心 提交于 2019-12-14 03:09:10

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!