可以添加额外的字段到@ManyToMany Hibernate额外的表? [英] Can add extra field(s) to @ManyToMany Hibernate extra table?

查看:226
本文介绍了可以添加额外的字段到@ManyToMany Hibernate额外的表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这两个类(表)

  @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))
私人设置< Student> student = new HashSet< Student>();
//一些setter和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>();
//一些setter和getter

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

解决方案

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



根据以下方法,链接表(STUDENT_COURSE)的行为类似于@Embeddable:

  @Embeddable 
public class JoinedStudentCourse {

//假设你已经添加了这个字段
@列(updatable = false)
私人日期joinedDate;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name =STUDENT_ID,insertable = false,updatable = false)
私立学生;
$ b @ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name =COURSE_ID,insertable = false,updatable = false)
私人课程;

// getter's和setter's

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

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

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

if(!(course.getId ).equals(other.getCourse()。getId()))
返回false;

// ATT:在equals()中使用诸如joinedDate这样的不可变字段实现
if !(joinedDate.equals(other.getJoinedDate()))
返回false;

返回true;
}

public int hashcode(){
//哈希码实现
}

}

所以你必须在学生和课程课上

  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设置< JoinedStudentCourse> joined = new HashSet< JoinedStudentCourse>();


$ / code>

请记住:@Embeddable类的生命周期必须拥有



建议:Hibernate团队支持这两种方法(@OneToMany(skaffman's answer)或@CollectionsOfElements)一些限制@ManyToMany映射 - 级联操作。



问候,


I have these two class(table)

@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

and this one:

    @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

After running this code an extra table was created in database(student_course), now I wanna know how can I add extra field in this table like (Grade, Date , and ... (I mean student_course table)) I see some solution but I don't like them, Also I have some problem with them:

First Sample

解决方案

If you add extra fields on a linked table (STUDENT_COURSE), you have to choose an approach according to skaffman's answer or another as shown bellow.

There is an approach where the linked table (STUDENT_COURSE) behaves like a @Embeddable according to:

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

}

So you will have in both Student and Course classes

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>();

}

remember: @Embeddable class has its lifecycle bound to the owning entity class (Both Student and Course), so take care of it.

advice: Hibernate team suppports these two approachs (@OneToMany (skaffman's answer) or @CollectionsOfElements) due some limitations in @ManyToMany mapping - cascade operation.

regards,

这篇关于可以添加额外的字段到@ManyToMany Hibernate额外的表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆