Map Mapping fails if there are duplicate values [message #962945] |
Mon, 29 October 2012 12:57 |
Bastian Lemke Messages: 5 Registered: September 2012 |
Junior Member |
|
|
I'm trying to map a HashMap similar to the one that is specified as example 3 in the JavaDoc for @MapKeyJoinColumn:
@Entity
public class Student {
@Id int studentId;
...
@ManyToMany // students and courses are also many-many
@JoinTable(name="ENROLLMENTS",
joinColumns=@JoinColumn(name="STUDENT"),
inverseJoinColumns=@JoinColumn(name="SEMESTER"))
@MapKeyJoinColumn(name="COURSE")
Map<Course, Semester> enrollment;
...
}
The generated join table (generated with EclipseLink 2.3) has the following layout:
TABLE enrollments (
student_id bigint NOT NULL,
semester_id bigint NOT NULL,
course_id bigint,
CONSTRAINT enrollments_pkey PRIMARY KEY (student_id, semester_id)
)
Why is the primary key generated for Student and Semester and not for Student and Course? This doesn't make any sense in this case. With this primary key, a Student can participate in only one course per semester. 'student_id' and 'course_id' should be defined as primary key! This would also match the Java map definition (the key must be unique, but the same value may be assigned to different keys)
|
|
|
Re: Map Mapping fails if there are duplicate values [message #964755 is a reply to message #962945] |
Tue, 30 October 2012 19:27 |
|
EclipseLink currently processes a ManyToMany that uses a MapKeyJoinColumn the same as a traditional ManyToMany, so uses the source/target ids as the primary key. You can log a bug/enhancement request to have these types of mappings treated differently.
In general, you may be best off creating an Enrollment Entity that maps to the join table. This will give you better control of its persistence.
James : Wiki : Book : Blog : Twitter
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.02695 seconds