当我试图拯救人时,它不会保存任何人或城市。我不想保存或更新City对象。我只想更新/删除joinColumn。有没有办法做到这一点?
Person person = new Person();
person.setCities(...);
personDAO.save(person);
public class Person{
@OneToMany(fetch = FetchType.EAGER)
@JoinColumn(name = "city_id")
private List<City> cities;
}
public class City{
@Id
@Column(name = "city_id")
@GeneratedValue(generator = "system-uuid")
@GenericGenerator(name = "system-uuid", strategy = "org.hibernate.id.UUIDGenerator")
private String cityId;
}
答案 0 :(得分:0)
首先在城市一侧定义@ManyToOne映射:
@ManyToOne
@JoinColumn(name = "person_id")
private Person person;
然后添加mappedBy
属性并删除@JoinColumn:
@OneToMany(fetch = FetchType.EAGER, mappedBy = "person")
private List<City> cities;
当你保存/更新时,你必须在依赖关系的两边设置实体:
Person person = new Person();
person.setCities(...);
for(City city: cities){
city.setPerson(person);
}
personDAO.save(person);
同样适用于删除..您必须删除双方的实体引用。