2

我正在使用 Eclipselink 提供的 JPA 实体用 Java 开发学生管理器应用程序。我试图在两个实体之间实现多对多关系一段时间,但我遇到了一些奇怪的异常。

我的实现基于: 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 ...
}

学期作业.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 ...
}

学期作业PK.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 工具生成数据库表时出现异常。

提前感谢您的帮助,彼得。

4

1 回答 1

1

不确定这是唯一的问题,但是您的mappedBy属性错误。

中的学期列表Homework由 的homework字段映射SemesterHomework。不在semester场上。

中的作业列表Semester由 的semester字段映射SemesterHomework。不在homework场上。

如果您在SemesterHomework.

于 2012-10-28T14:34:43.517 回答