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.
Not sure this is the only problem, but you got your
mappedByattributes wrong.The list of semesters in
Homeworkis mapped by thehomeworkfield ofSemesterHomework. Not by thesemesterfield.And the list of homeworks in
Semesteris mapped by thesemesterfield ofSemesterHomework. Not by thehomeworkfield.Everything would be much easier if you had a non-composite, autogenerated ID in
SemesterHomework.