Skip to main content



      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 08:57 Go to next message
Eclipse UserFriend
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 15:27 Go to previous messageGo to next message
Eclipse UserFriend
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.
Re: Map Mapping fails if there are duplicate values [message #972024 is a reply to message #962945] Mon, 05 November 2012 04:47 Go to previous messageGo to next message
Eclipse UserFriend
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 05:18 Go to previous message
Eclipse UserFriend
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: Wed Jul 23 03:05:53 EDT 2025

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

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

Back to the top