Java playframework ebean中的自定义桥接表
我试图创建桥接表来解决两个@manytomy关系,但没有成功。但是,此表必须包含其他字段。例如:Java playframework ebean中的自定义桥接表,java,database,playframework,playframework-2.2,ebean,Java,Database,Playframework,Playframework 2.2,Ebean,我试图创建桥接表来解决两个@manytomy关系,但没有成功。但是,此表必须包含其他字段。例如: Course: -course_id - pk Student: -student_id -pk Bridge: -(course_id, student_id) - pk -additional_field 我的学生课是这样的: @Entity public class Student extends Model { @Id @OneToMany p
Course: -course_id - pk
Student: -student_id -pk
Bridge: -(course_id, student_id) - pk
-additional_field
我的学生课是这样的:
@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类中添加了normal@Id字段,与学生和课程的关系是normal关系。 此解决方案在数据库的“bridge”表中包含一个附加的“id”字段 代码如下: 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
);
}
另外还有一个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;
}
}
本规范中更重要的是:
上述解决方案有几个变通方法。但我没能找到更简单更干净的。为什么桥梁列表是主键?主键不应该是long或int吗?@rtruszk这就是我在ebean用户指南中读到的,OneToMany关系完成了。逻辑上是的,我希望pk为int,但我不知道如何将其链接到桥接表。请注意,“桥接”表通常被称为“交叉点”表,因此只需在此处添加此注释以帮助将来的谷歌搜索。当我这样做时,我会得到Beanlist延迟。您如何查询?您的网桥中的id字段是什么?这是一个附加字段还是一个附加字段?
@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
);
}
@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;
}
}
@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;
}
}