在playframework ebean的自定义桥梁桌 [英] Custom bridge table in playframework ebean
问题描述
我没有成功尝试创建桥表,将解决两个@ManyToMany关系。但是此表必须包含其他字段。例如:
I am unsuccessfuly trying to create bridge table that would resolve two @ManyToMany relations. However this table have to contain additional field. For example:
Course: -course_id - pk
Student: -student_id -pk
Bridge: -(course_id, student_id) - pk
-additional_field
我的学生类看起来像这样:
My student class looks like this:
@Entity
public class Student extends Model {
@Id
@OneToMany
public List<Bridge> student_id;
}
课程类别基本一样。
桥接表格如下:
@Entity
public class Bridge extends Model{
@EmbeddedId
public compound_key student_course;
public String additional_field;
@Embeddable
public class compound_key{
@ManyToOne
public Student student_id;
@ManyToOne
public Student course_id;
}
}
帮助。
推荐答案
我找到了以下解决方案。这是一个在Bridge中没有复合键的解决方案。我在Bridge类中添加了正常的@Id字段,并且关系与Student和Course是正常关系。
此解决方案在数据库的bridge表中包含一个附加的id字段。
I have found the following solution. This is a solution without a composite key in Bridge. I added normal @Id field in Bridge class and relations to Student and Course are normal relations. This solution contains an additional 'id' field in the 'bridge' table in the database.
这是代码:
Student.java:
Student.java:
@Entity
public class Student extends Model {
@Id
public Integer id;
@OneToMany(mappedBy="student")
public List<Bridge> bridges;
public static Finder<Integer,Student> find = new Finder<Integer,Student>(
Integer.class, Student.class
);
}
Course.java:
Course.java:
@Entity
public class Course extends Model {
@Id
public Integer id;
@OneToMany(mappedBy="course")
public List<Bridge> bridges;
public static Finder<Integer,Course> find = new Finder<Integer,Course>(
Integer.class, Course.class
);
}
Bridge.java:
Bridge.java:
@Entity
public class Bridge extends Model {
@Id
public Integer id;
@ManyToOne public Student student;
@ManyToOne public Course course;
public String additional_field;
public static Finder<Integer,Bridge> find = new Finder<Integer,Bridge>(
Integer.class, Bridge.class
);
}
EDIT
经过多次尝试,我在Bridge类中找到了带有复合键的解决方案。课程学生和课程与以前的解决方案相同。
After many attempts I have found solution with composite key in Bridge class. Classes Student and Course are the same as in previous solution.
Bridge.java更改为以下内容:
Bridge.java changed to following:
@Entity
public class Bridge extends Model {
Bridge() {
bridgeId = new BridgeId();
}
@EmbeddedId
protected BridgeId bridgeId;
@ManyToOne
@JoinColumn(name = "student_id", insertable = false, updatable = false)
private Student student;
@ManyToOne
@JoinColumn(name="course_id", insertable = false, updatable = false)
private Course course;
public String additional_field;
public Student getStudent() {
return student;
}
public void setStudent(Student aStudent) {
student=aStudent;
bridgeId.student_id = aStudent.id;
}
public Course getCourse() {
return course;
}
public void setCourse(Course aCourse){
course=aCourse;
bridgeId.course_id = aCourse.id;
}
}
还有额外的BridgeId.java:
And there is additional BridgeId.java:
@Embeddable
public class BridgeId implements Serializable
{
public Integer student_id;
public Integer course_id;
public int hashCode() {
return student_id + course_id;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
BridgeId b = (BridgeId)obj;
if(b==null)
return false;
if (b.student_id == student_id && b.course_id == course_id) {
return true;
}
return false;
}
}
在此代码中更重要的是: p>
What is more important in this code is:
- 嵌入式ID的字段映射到与ManyToOne关系相同的列。
- student_id和course_id值的值从嵌入式ID而不是关系中插入。这是因为关系的属性可插入和可更新设置为false。
- 我必须为student和course字段添加getter和setters。在设置我更新嵌入键的字段。
上面的解决方案有几个解决方法。但我不能找到更容易和更清洁的一个。
Above solution has several workarounds. But I wasn't ableto find easier and cleaner one.
这篇关于在playframework ebean的自定义桥梁桌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!