Eclipselink が提供する JPA エンティティを使用して、Java で学生管理アプリケーションを開発しています。しばらくの間、2 つのエンティティ間に多対多の関係を実装しようとしていますが、いくつかの奇妙な例外に遭遇しました。
私の実装は以下に基づいています: ManyToMany 関係の Java 永続性 Wiki ページと、この StackOverflow 投稿。
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 ...
}
学期.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 ツールを使用してデータベース テーブルを生成しようとすると、例外が発生します。
前もって助けてくれてありがとう、ピーター。