简体   繁体   中英

Hibernate not inserting row in join table used in @onetomany bi-directional relationship.

I have two pojo student and section. relation is that , one section has many students. (bi-directional)

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

and

@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 and section table is populating but join table "student_section" is not populating. Please provide solution. thanks in advance.

Could you please try with the following mappings :

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

in Section class and following in Student class :

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

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM