Eclipselink 2.4 + JPA + ManyToMany带有附加列 [英] Eclipselink 2.4 + JPA + ManyToMany with additional column
问题描述
我正在使用Eclipselink提供的JPA实体以Java开发学生管理器应用程序。我试图在两个实体之间实现多对多关系一段时间,但遇到了一些奇怪的例外。
I'm developing a student manager application in Java with JPA entities provided by Eclipselink. I'm trying to implement a many-to-many relationship between two entities for a while, but I ran into some weird exceptions.
我的实现基于: b $ b ManyToMany关系的Java持久性Wiki页面和此StackOverflow帖子。
My implementations are based on: Java persistence Wiki page of ManyToMany relationship and this StackOverflow post.
这是我的 Homework.java 文件的相关部分:
Here is my Homework.java file's relevant parts:
@Entity
@Table(name = "db_homework")
public class Homework {
@Id
@GeneratedValue(strategy = IDENTITY)
private long id;
... other fields and connections ...
@OneToMany(mappedBy = "semester")
private Collection<SemesterHomework> semesters;
... getters/setters ...
}
Semester.java:
@Entity
@Table(name = "db_semester")
public class Semester {
@Id
@GeneratedValue(strategy = IDENTITY)
private long id;
... other fields and connections ...
@OneToMany(mappedBy = "homework")
private Collection<SemesterHomework> homeworks;
... getters/setters ...
}
SemesterHomework.java:
@Entity
@Table(name="db_semesterhomework")
@IdClass(SemesterHomeworkPK.class)
public class SemesterHomework {
@Id
@ManyToOne(optional=false)
@PrimaryKeyJoinColumn(name="HOMEWORK_ID", referencedColumnName="ID")
private Homework homework;
@Id
@ManyToOne(optional=false)
@PrimaryKeyJoinColumn(name="SEMESTER_ID", referencedColumnName="ID")
private Semester semester;
@Column
@Temporal(TemporalType.TIMESTAMP)
private Date finishTime;
... getters/setters ...
}
SemesterHomeworkPK.java:
public class SemesterHomeworkPK {
@Id
@Column(name="SEMESTER_ID")
private Long semester;
@Id
@Column(name="HOMEWORK_ID")
private Long homework;
@Override
public boolean equals(Object arg0) {
...
}
@Override
public int hashCode() {
...
}
... getters/setters ...
}
我得到的例外:
Exception [EclipseLink-41] (Eclipse Persistence Services - 2.4.0.v20120608-r11652): org.eclipse.persistence.exceptions.DescriptorException
Exception Description: A non-read-only mapping must be defined for the sequence number field.
Descriptor: RelationalDescriptor(org.bme.amorg.droidhfsite.server.db.Homework --> [DatabaseTable(db_homework)])
Exception [EclipseLink-41] (Eclipse Persistence Services - 2.4.0.v20120608-r11652): org.eclipse.persistence.exceptions.DescriptorException
Exception Description: A non-read-only mapping must be defined for the sequence number field.
Descriptor: RelationalDescriptor(org.bme.amorg.droidhfsite.server.db.Semester --> [DatabaseTable(db_semester)])
Exception [EclipseLink-93] (Eclipse Persistence Services - 2.4.0.v20120608-r11652): org.eclipse.persistence.exceptions.DescriptorException
Exception Description: The table [db_homework] is not present in this descriptor.
Descriptor: RelationalDescriptor(org.bme.amorg.droidhfsite.server.db.Semester --> [DatabaseTable(db_semester)])
Exception [EclipseLink-93] (Eclipse Persistence Services - 2.4.0.v20120608-r11652): org.eclipse.persistence.exceptions.DescriptorException
Exception Description: The table [db_semester] is not present in this descriptor.
Descriptor: RelationalDescriptor(org.bme.amorg.droidhfsite.server.db.Homework --> [DatabaseTable(db_homework)])
当我尝试使用Eclipse JPA工具生成数据库表时,会发生异常。
The exceptions occur when I try to generate the database tables with Eclipse JPA tools.
感谢您的帮助,彼得
。
Thanks for the help in advance, Peter.
推荐答案
不确定这是唯一的问题,但您的 mappedBy
属性有误。
Not sure this is the only problem, but you got your mappedBy
attributes wrong.
家庭作业
中的学期列表由家庭作业$ c $映射
SemesterHomework
的c>字段。 学期
字段除外。
The list of semesters in Homework
is mapped by the homework
field of SemesterHomework
. Not by the semester
field.
学期
中的家庭作业列表由学期$ c映射
SemesterHomework
的$ c>字段。 家庭作业
字段除外。
And the list of homeworks in Semester
is mapped by the semester
field of SemesterHomework
. Not by the homework
field.
如果您在 SemesterHomework
中拥有非合成的自动生成的ID,一切都会变得容易得多。
Everything would be much easier if you had a non-composite, autogenerated ID in SemesterHomework
.
这篇关于Eclipselink 2.4 + JPA + ManyToMany带有附加列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!