我正在使用Eclipselink提供的JPA实体开发Java学生管理器应用程序。我试图在一段时间内实现两个实体之间的多对多关系,但我遇到了一些奇怪的例外。
我的实施基于: Java persistence Wiki page of ManyToMany relationship和this StackOverflow post。
这是我的 Homework.java 文件的相关部分:
@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工具生成数据库表时会发生异常。
感谢您的帮助, 彼得。
答案 0 :(得分:1)
不确定这是唯一的问题,但您的mappedBy
属性错误。
Homework
中的学期列表由homework
的{{1}}字段映射。不是SemesterHomework
字段。
semester
中的作业列表由Semester
的{{1}}字段映射。不是semester
字段。
如果您在SemesterHomework
中拥有非复合的,自动生成的ID,那么一切都会轻松得多。