EntityExistsException persisting a second entity with @MapsId while a sibling-class is in the persistence context

StackOverflow https://stackoverflow.com/questions/18058779

  •  23-06-2022
  •  | 
  •  

Question

I've got a curious problem I can't really work out. It's this exception:

javax.persistence.EntityExistsException: a different object with the same identifier value was already associated with the session: [com.mycompany.Employee#101]
    at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1359) [hibernate-entitymanager-4.2.2.Final.jar:4.2.2.Final]
    at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1310) [hibernate-entitymanager-4.2.2.Final.jar:4.2.2.Final]
    at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1316) [hibernate-entitymanager-4.2.2.Final.jar:4.2.2.Final]
    at org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:881) [hibernate-entitymanager-4.2.2.Final.jar:4.2.2.Final]

The exception is pretty clear that there's an object in the persistence context that prevents my persist from going through. The problem is: there's no Employee in the context at all (checked with a debugger)!

However, there's the Talent instance I want to update to also be an Employee. They are related like this:

A person:

public class Person {
    @Id @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @OneToMany(mappedBy = "person")
    private Set<PersonRole> roles = new LinkedHashSet<>();
}

has many roles:

@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class PersonRole {
    @Id @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "personrolegen")
    @SequenceGenerator(name = "personrolegen", sequenceName = "person_role_id_seq")
    protected Long id;

    @MapsId
    @ManyToOne(optional = false)
    protected Person person;
}

and both Talent and Employee extend PersonRole (nothing special to see there).

The exception occurs when a person already is a talent and I construct an employee with a reference to an existing person and call

entityManager.persist(new Employee(person))

Now the @Id mapping in PersonRole doesn't look very sound, can somebody confirm that it's the problem causing the exception above? And how can I do better?

What I wanted to express is that each role has a person attached to it that uniquely identifies the child instance of the role. Looking at the Persistence Context it seems like Hibernate is using PersonRole#person.id as the EntityKey, which seems to cause the exception.

Was it helpful?

Solution

Typing it all down helped answering the question myself:

I don't want @MapsId on PersonRole#person, since a role shouldn't be identified by the person alone. In fact, if the role could be identified by the person, it's just another property of the person and not a new role.

So my original mapping design was faulty. Leaving off the @MapsId makes the exception go away.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top