Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » Map Mapping fails if there are duplicate values
Map Mapping fails if there are duplicate values [message #962945] Mon, 29 October 2012 12:57 Go to next message
Bastian Lemke is currently offline Bastian LemkeFriend
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 Go to previous messageGo to next message
James Sutherland is currently offline James SutherlandFriend
Messages: 1939
Registered: July 2009
Location: Ottawa, Canada
Senior Member

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
Re: Map Mapping fails if there are duplicate values [message #972024 is a reply to message #962945] Mon, 05 November 2012 09:47 Go to previous messageGo to next message
Bastian Lemke is currently offline Bastian LemkeFriend
Messages: 5
Registered: September 2012
Junior Member
Thanks for your reply. As you suggested, I created an Entity that maps to the join table. But now I have a Set/List instead of a map. I found no way to use a map.

IMO the generated database constraints are wrong. What happens, if I have multiple records with the same combination of student/course in the database?

I'll submit a bug...
Re: Map Mapping fails if there are duplicate values [message #972056 is a reply to message #962945] Mon, 05 November 2012 10:18 Go to previous message
Bastian Lemke is currently offline Bastian LemkeFriend
Messages: 5
Registered: September 2012
Junior Member
https://bugs.eclipse.org/bugs/show_bug.cgi?id=393526
Previous Topic:JPA Project with EclipseLink 2.4.1 Error-Msg
Next Topic:MOXy JAXB with dynamic entities
Goto Forum:
  


Current Time: Sat Nov 22 21:22:23 GMT 2014

Powered by FUDForum. Page generated in 0.01639 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software