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, f开发者_如何学编程etch=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.
精彩评论