【问题标题】:Hibernate not inserting row in join table used in @onetomany bi-directional relationship.Hibernate 不在@onetomany 双向关系中使用的连接表中插入行。
【发布时间】:2015-07-28 12:45:49
【问题描述】:

我有两个 pojo 学生和部门。 关系是,一节有很多学生。 (双向)

@Entity
@Table(name="section")
public class Section {

@Id
@GeneratedValue
private int sectionId;
private String sectionName;
private int maxStudent;

@OneToMany(cascade=CascadeType.PERSIST)
@JoinTable(name="student_section" , inverseJoinColumns={@JoinColumn(name="student_id")},joinColumns={@JoinColumn(name="section_id")})
private Set<Student> student = new HashSet<Student>(0);

public Set<Student> getStudent() {
    return student;
}
public void setStudent(Set<Student> student) {
    this.student = student;
}

@Entity
@Table(name="student")
public class Student {

@Id
@GeneratedValue
private int studentId;
private String studentName;
private int studentAge;
@OneToOne(cascade=CascadeType.PERSIST)
@JoinTable(name="student_section" , joinColumns={@JoinColumn(name="student_id")},inverseJoinColumns={@JoinColumn(name="section_id")})
private Section section;

正在填充学生和分区表,但未填充连接表“student_section”。 请提供解决方案。 提前致谢。

【问题讨论】:

    标签: java hibernate hibernate-annotations


    【解决方案1】:

    请您尝试以下映射:

    @OneToMany(cascade=CascadeType.PERSIST)
    @JoinTable(name="student_section" , inverseJoinColumns={@JoinColumn(name="student_id")},joinColumns={@JoinColumn(name="section_id")})
    private Set<Student> student
    

    在Section类和Student类中:

    @ManytoOne(cascade=CascadeType.ALL)
     private Section section;
    

    【讨论】:

      猜你喜欢
      • 2013-05-09
      • 2016-05-16
      • 1970-01-01
      • 2013-07-06
      • 1970-01-01
      • 1970-01-01
      • 2013-06-17
      • 2018-05-13
      • 1970-01-01
      相关资源
      最近更新 更多