gpt4 book ai didi

spring - 如何为连接表中的其他属性创建多对多 Hibernate 映射?

转载 作者:IT老高 更新时间:2023-10-28 13:50:53 25 4
gpt4 key购买 nike

我需要一个多对多 hibernate 映射,需要 3 个连接。我试图找出一个没有像 LecturerCourse 这样的中间实体的解决方案。

我的数据库中的讲师和类(class)表之间存在多对多关系。一个类(class)可以由多个讲师讲授,而一个讲师可以讲多个类(class)。

我已经预先存储了类(class)。但是,我需要将类(class)分配给讲师。当我分配类(class)时,我还会存储该类(class)的容量。

我的数据库图:

enter image description here

我使用 hibernate 和 Spring 。当类(class)分配任何讲师时,我需要一个 hibernate 映射。我需要向容量字段添加值。

我的讲师映射:

@Entity
@Table(name="LECTURER")
public class Lecturer {

@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="LECTURER_ID_SEQ")
@SequenceGenerator(name="LECTURER_ID_SEQ", sequenceName="LECTURER_ID_SEQ")
private Long Id;

@Column(name="NAME")
private String name;

@Column(name="SURNAME")
private String surname;


@Column(name="EMAIL")
private String email;

@Column(name="USERNAME")
private String username;

@Column(name="PASSWORD")
private String Password;

@ManyToMany
@JoinTable(
name="LECTURER_COURSE",
joinColumns=@JoinColumn(name="LECTURER_ID"),
inverseJoinColumns=@JoinColumn(name="COURSE_ID")
)
private List<Course> courses;

//getters - setters
}

我的类(class)映射:

@Entity
@Table(name="COURSE")
public class Course {

@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="COURSE_ID_SEQ")
@SequenceGenerator(name="COURSE_ID_SEQ", sequenceName="COURSE_ID_SEQ")
private Long id;

@Column(name="NAME")
private String name;

@Column(name="CODE")
private String code;
}

知道如何解决我的问题吗?

最佳答案

你需要使用@EmbeddedId@Embeddable注解来解决这个问题:

讲师类:

@Entity
@Table(name="LECTURER")
public class Lecturer {

@OneToMany(fetch = FetchType.LAZY, mappedBy = "pk.lecturer", cascade=CascadeType.ALL)
Set<LecturerCourse> lecturerCourses == new HashSet<LecturerCourse>();

//all others properties Setters and getters are less relevant.

}

类(class)类别:

@Entity
@Table(name="COURSE")
public class Course {

@OneToMany(fetch = FetchType.LAZY, mappedBy = "pk.course", cascade=CascadeType.ALL)
Set<LecturerCourse> lecturerCourses == new HashSet<LecturerCourse>();

//all others properties Setters and getters are less relevant.

}

讲师类(class)类:

@Entity
@Table(name = "lecturer_course")
@AssociationOverrides({
@AssociationOverride(name = "pk.lecturer",
joinColumns = @JoinColumn(name = "LECTURER_ID")),
@AssociationOverride(name = "pk.course",
joinColumns = @JoinColumn(name = "COURSE_ID")) })
public class LecturerCourse {

private LecturerCourseID pk = new LecturerCourseID();

@Column(name = "CAPACITY", nullable = false, length = 10)
private String capacity;

@EmbeddedId
public LecturerCourseID getPk() {
return pk;
}

}

现在是主键:

@Embeddable
public class LecturerCourseID implements java.io.Serializable {

private Lecturer lecturer;
private Course course;

@ManyToOne
public Stock getLecturer() {
return lecturer;
}

public void setLecturer(Lecturer lecturer) {
this.lecturer= lecturer;
}

@ManyToOne
public Course getCourse() {
return course;
}

public void setCourse(Course course) {
this.course= course;
}

}

现在你的 Main 应该是这样的:

Lecturer lecturer1 = new Lecturer();
Course math = new Course();
LecturerCourse lecturer1math = new LecturerCourse();
lecturer1math.setCapacity("capacity");
lecturer1math.setLecturer(lecturer1);
lecturer1math.setCourse(math);
lecturer1.getLecturerCourses().add(lecturer1math);

//saving object
session.save(lecturer1);

您需要确保标记为 @Embeddable 的类应该实现 Serializable 标记接口(interface)。

希望对你有帮助。

关于spring - 如何为连接表中的其他属性创建多对多 Hibernate 映射?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10294338/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com