When I try to save person, it doesn't save anything neither person nor city. I don't want to save or update City objects. I just want to update/remove joinColumn. Is there any way to do this?
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;
}
First define the @ManyToOne mapping on the City side:
@ManyToOne
@JoinColumn(name = "person_id")
private Person person;
Then add mappedBy
attribute and remove @JoinColumn:
@OneToMany(fetch = FetchType.EAGER, mappedBy = "person")
private List<City> cities;
and when you save / update you have to set the entities on both sides of the dependency:
Person person = new Person();
person.setCities(...);
for(City city: cities){
city.setPerson(person);
}
personDAO.save(person);
The same goes for removal.. you have to remove the entity references on both sides.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.