Hibernate没有在onetomany关系中插入列

时间:2011-03-07 06:09:07

标签: hibernate spring spring-mvc one-to-many

我有两张桌子人

@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;
 }

一个人可以拥有多本护照:)

护照

@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;

}
护照表中的

除了person_id

外,一切都被插入

1 个答案:

答案 0 :(得分:5)

在保存人员之前:

for(Passport passport : person.getPassports())
{
    passport.setPerson(person);
}

repo.save(person);

必须明确设置Hibernate中的双向关系。