Eclipselink 2.4 + JPA + ManyToMany附加列

时间:2012-10-28 14:05:42

标签: jpa jpa-2.0 eclipselink

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

我的实施基于: Java persistence Wiki page of ManyToMany relationshipthis 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工具生成数据库表时会发生异常。

感谢您的帮助, 彼得。

1 个答案:

答案 0 :(得分:1)

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

Homework中的学期列表由homework的{​​{1}}字段映射。不是SemesterHomework字段。

semester中的作业列表由Semester的{​​{1}}字段映射。不是semester字段。

如果您在SemesterHomework中拥有非复合的,自动生成的ID,那么一切都会轻松得多。