Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 350510

Summary: Duplicate entity in persistence context when using an entity as the MapKey
Product: z_Archived Reporter: Missing name Mising name <k_os_2003>
Component: EclipselinkAssignee: Nobody - feel free to take it <nobody>
Status: NEW --- QA Contact:
Severity: major    
Priority: P2 CC: k_os_2003, tom.ware
Version: unspecified   
Target Milestone: ---   
Hardware: PC   
OS: Windows 7   
Whiteboard:

Description Missing name Mising name CLA 2011-06-27 22:16:12 EDT
Build Identifier: 2.2.0

My JPA provider is EclipseLink JPA 2.2.0 distributed with Glassfish 3.1.

This is the problematic set of JPA entities :
@Entity
public class EventAttendance {
	...
	@ManyToOne private Person person;
	@ManyToOne private Event event;
	...
}

@Entity
public class Event {
	...
	@OneToMany(mappedBy = "event")
	@MapKey(name = "person")
	private Map<Person, EventAttendance> attendance;
	...
}

@Entity
public class Person { ... }


This is a set of operation that gives erroneous results:
BeginTransaction();
EntityManager em = ...;
Person person = em.find(Person.class, PERSON_ID);
Event event = em.find(Event.class, EVENT_ID);

// This map contains at least the entry:
// Key = Person with id == PERSON_ID
// Value = an instance of EventAttendance
Map<Person, EventAttendance> attendanceMap = event.getAttendance();

// Should return the EventAttendance instance mentioned above, but returns null!
EventAttendance attendance = attendanceMap.get(person);
CommitTransaction();


Digging around in the debugger, I found that the problem is that there exists 2 instances of the same Person entity in the persistence context. These 2 instances reference the same entity which violates the JPA contract.

Changing the Map to a simple List gives the expected behavior whereas the Person instance with id PERSON_ID is shared between the List in Event and em.find(Person.class, PERSON_ID).


Reproducible: Always
Comment 1 Tom Ware CLA 2011-07-08 11:10:29 EDT
Setting target and priority.  See the following page for the meanings of these fields:

http://wiki.eclipse.org/EclipseLink/Development/Bugs/Guidelines

Community: Please vote for this bug if it is important to you.  Votes are one of the main criteria we use to determine which bugs to fix next.
Comment 2 Eclipse Webmaster CLA 2022-06-09 10:26:04 EDT
The Eclipselink project has moved to Github: https://github.com/eclipse-ee4j/eclipselink