We're updating the issue view to help you get more done. 

The modification of a @OneToOne with @MapsId property is silently ignored during a merge operation

Description

Creating issue for discussion here at https://discourse.hibernate.org/t/hibernate-onetoone-with-mapsid-does-not-map-the-same-id-with-the-parent-entity/2093/9

Summary:
A child entity is mapped using @OneToOne with @MapsId to its parent entity.

For eg,

1 2 3 4 5 6 7 class Parent { } class Child { @OneToOne @MapsId private Parent parent; }

The assumption is that child will refer to its correct parent in any case. However, following code will break this assumption.

1 2 3 4 5 6 7 8 Parent p = new Parent(); // new parent em.save(p); em.flush(); Child c = em.find(Child.class, 1L); // child already have its original parent during its creation time c.setParent(p); // updating to new parent Child result = em.merge(c); result.getParent(); // new parent

This way it will continue to use new parent until the end of transaction.

@OneToOne with @MapsId should always resolves to correct association value.

Sample test cases which show the issue.
https://github.com/pmverma/hibernate-mapsid-test
https://github.com/vladmihalcea/high-performance-java-persistence/pull/36

Environment

Hibernate Version: 5.2.17
java version "1.8.0_181"
Java(TM) SE Runtime Environment (build 1.8.0_181-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.181-b13, mixed mode)
Windows 10
Postgresql 10.4

Status

Assignee

Vlad Mihalcea

Reporter

P. Mohan

Fix versions

Labels

backPortable

None

Suitable for new contributors

None

Requires Release Note

None

Pull Request

None

backportDecision

None

Components

Affects versions

5.2.18

Priority

Major