Bug 350510 - Duplicate entity in persistence context when using an entity as the MapKey
Summary: Duplicate entity in persistence context when using an entity as the MapKey
Status: NEW
Alias: None
Product: z_Archived
Classification: Eclipse Foundation
Component: Eclipselink (show other bugs)
Version: unspecified   Edit
Hardware: PC Windows 7
: P2 major (vote)
Target Milestone: ---   Edit
Assignee: Nobody - feel free to take it CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-06-27 22:16 EDT by Missing name Mising name CLA
Modified: 2022-06-09 10:26 EDT (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
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