Eclipselink 2.4 + JPA + ManyToMany with additional column

和自甴很熟 提交于 2019-12-24 03:49:15

问题


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.

My implementations are based on: Java persistence Wiki page of ManyToMany relationship and this StackOverflow post.

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 ...
}

The exceptions what i get:

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)])

The exceptions occur when I try to generate the database tables with Eclipse JPA tools.

Thanks for the help in advance, Peter.


回答1:


Not sure this is the only problem, but you got your mappedBy attributes wrong.

The list of semesters in Homework is mapped by the homework field of SemesterHomework. Not by the semester field.

And the list of homeworks in Semester is mapped by the semester field of SemesterHomework. Not by the homework field.

Everything would be much easier if you had a non-composite, autogenerated ID in SemesterHomework.



来源:https://stackoverflow.com/questions/13109463/eclipselink-2-4-jpa-manytomany-with-additional-column

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!