gpt4 book ai didi

java - 可以向@ManyToMany Hibernate 额外表添加额外字段吗?

转载 作者:太空狗 更新时间:2023-10-29 23:03:05 24 4
gpt4 key购买 nike

我有这两个类(表)

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

@Id
@Column(name = "courseid")
private String courseId;
@Column(name = "coursename")
private String courseName;
@Column(name = "vahed")
private int vahed;
@Column(name = "coursedep")
private int dep;
@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(name = "student_course", joinColumns = @JoinColumn(name = "course_id"), inverseJoinColumns = @JoinColumn(name = "student_id"))
private Set<Student> student = new HashSet<Student>();
//Some setter and getter

还有这个:

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

@Id
@Column(name="studid")
private String stId;
@Column(nullable = false, name="studname")
private String studName;
@Column(name="stmajor")
private String stMajor;
@Column(name="stlevel", length=3)
private String stLevel;
@Column(name="stdep")
private int stdep;

@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(name = "student_course"
,joinColumns = @JoinColumn(name = "student_id")
,inverseJoinColumns = @JoinColumn(name = "course_id")
)
private Set<Course> course = new HashSet<Course>();
//Some setter and getter

运行此代码后,在数据库 (student_course) 中创建了一个额外的表,现在我想知道如何在此表中添加额外的字段,例如(年级、日期和...(我的意思是 student_course 表))我看到了一些解决方案,但我不喜欢它们,而且我对它们也有一些问题:

First Sample

最佳答案

如果您在链接表 (STUDENT_COURSE) 上添加额外字段,则必须根据 skaffman 的回答或如下所示选择另一种方法。

有一种方法可以使链接表 (STUDENT_COURSE) 的行为类似于 @Embeddable,根据:

@Embeddable
public class JoinedStudentCourse {

// Lets suppose you have added this field
@Column(updatable=false)
private Date joinedDate;

@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="STUDENT_ID", insertable=false, updatable=false)
private Student student;

@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="COURSE_ID", insertable=false, updatable=false)
private Course course;

// getter's and setter's

public boolean equals(Object instance) {
if(instance == null)
return false;

if(!(instance instanceof JoinedStudentCourse))
return false;

JoinedStudentCourse other = (JoinedStudentCourse) instance;
if(!(student.getId().equals(other.getStudent().getId()))
return false;

if(!(course.getId().equals(other.getCourse().getId()))
return false;

// ATT: use immutable fields like joinedDate in equals() implementation
if(!(joinedDate.equals(other.getJoinedDate()))
return false;

return true;
}

public int hashcode() {
// hashcode implementation
}

}

所以你将同时拥有 Student 和 Course 类

public class Student {

@CollectionOfElements
@JoinTable(
table=@Table(name="STUDENT_COURSE"),
joinColumns=@JoinColumn(name="STUDENT_ID")
)
private Set<JoinedStudentCourse> joined = new HashSet<JoinedStudentCourse>();

}

public class Course {

@CollectionOfElements
@JoinTable(
table=@Table(name="STUDENT_COURSE"),
joinColumns=@JoinColumn(name="COURSE_ID")
)
private Set<JoinedStudentCourse> joined = new HashSet<JoinedStudentCourse>();

}

记住:@Embeddable 类的生命周期绑定(bind)到拥有的实体类(学生和类(class)),所以要小心。

建议:Hibernate 团队支持这两种方法(@OneToMany(skaffman 的回答)或@CollectionsOfElements),因为@ManyToMany 映射 - 级联操作存在一些限制。

问候,

关于java - 可以向@ManyToMany Hibernate 额外表添加额外字段吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1153409/

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