栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

可以在@ManyToMany Hibernate额外表中添加额外字段吗?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

可以在@ManyToMany Hibernate额外表中添加额外字段吗?

如果在链接表(STUDENT_COURSE)上添加额外的字段,则必须根据skaffman的答案或以下所示选择其他方法。

有一种方法可以根据以下条件使链接表(STUDENT_COURSE)表现为@Embeddable:

@Embeddablepublic 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 hashpre() {        // hashpre implementation    }}

因此,您将同时参加学生和课程课程

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类的生命周期绑定到拥有的实体类(Student和Course),因此请多加注意。

建议:由于@ManyToMany映射中的某些限制-
级联操作,Hibernate团队支持这两种方法(@OneToMany(skaffman的答案)或@CollectionsOfElements)。

问候,



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/417529.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号