I have two tables Person
@Id
@Column(name = "PERSON_ID")
@GeneratedValue
public Integer getId() {
return id;
}
@Column(name = "FIRST_NAME")
public String getFirstName() {
return firstName;
}
@Column(name = "LAST_NAME")
public String getLastName() {
return lastName;
}
@Column(name = "MONEY")
public Double getMoney() {
return money;
}
@OneToMany(cascade = CascadeType.ALL, fetch=FetchType.LAZY, mappedBy="person")
@JoinColumn(name="person_id")
public List<Passport> getPassports(){
return this.passports;
}
One person can have many passports :)
Passport
@Id
@Column(name = "passport_id")
@GeneratedValue
public Integer getPassport_id() {
return passport_id;
}
@Column(name = "country_issue")
public String getCountry_issue() {
return country_issue;
}
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="person_id")
public Person getPerson(){
return person;
}
in passport table everything is inserted except person_id
Before saving Person:
for(Passport passport : person.getPassports())
{
passport.setPerson(person);
}
repo.save(person);
Bi-direcitonal relationships in Hibernate has to be setup explicitly.
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.