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.