# Hibernate @OneToOne with MapsId does not map the same id with the parent entity

**URL:** <https://discourse.hibernate.org/t/hibernate-onetoone-with-mapsid-does-not-map-the-same-id-with-the-parent-entity/2093>\
**Category:** Hibernate ORM\
**Created:** [January 19, 2019, 6:27pm UTC](https://discourse.hibernate.org/t/hibernate-onetoone-with-mapsid-does-not-map-the-same-id-with-the-parent-entity/2093 "2019-01-19T18:27:17Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![pmverma](https://avatars.discourse-cdn.com/v4/letter/p/e47c2d/32.png) [@pmverma](https://discourse.hibernate.org/u/pmverma)\
**Post date:** [January 19, 2019, 6:27pm UTC](https://discourse.hibernate.org/t/hibernate-onetoone-with-mapsid-does-not-map-the-same-id-with-the-parent-entity/2093/1 "2019-01-19T18:27:17Z")

</div>

Hi,

I have a child entity of which id is mapped with parent id using `@MapsId @OneToOne`.

I am running Mock Tests with JUnit to confirm the correct behavior of `@MapsId @OneToOne`, however I am getting difference ids for parent and child, during update case.

I have created a simple project which shows this error.

[https://github.com/pmverma/hibernate-mapsid-test](https://github.com/pmverma/hibernate-mapsid-test)

Is this correct behavior?  
What I am doing wrong?

Any help is really appreciated.

Regards,  
Mohan

---

<div class="post-metadata">

**Author:** ![vlad](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/vlad/32/658_2.png) [@vlad](https://discourse.hibernate.org/u/vlad)\
**Post date:** [January 19, 2019, 6:47pm UTC](https://discourse.hibernate.org/t/hibernate-onetoone-with-mapsid-does-not-map-the-same-id-with-the-parent-entity/2093/2 "2019-01-19T18:47:09Z")

</div>

Your test is not correct. How could a Child be created without a Parent?

```auto
childRepository.saveAndFlush(child);

Parent parent = new Parent();
em.persist(parent);
em.flush();

```

What would the Child id point to?

Also, you don’t need to Mock the DB. Just use an integration test instead.

[This test](https://github.com/hibernate/hibernate-orm/blob/master/documentation/src/test/java/org/hibernate/userguide/associations/OneToOneMapsIdTest.java) in the Hibernate ORM project shows that everything works just fine. Try to debug the Hibernate test and see why that one works and yours does not.

---

<div class="post-metadata">

**Author:** ![pmverma](https://avatars.discourse-cdn.com/v4/letter/p/e47c2d/32.png) [@pmverma](https://discourse.hibernate.org/u/pmverma)\
**Post date:** [January 19, 2019, 7:20pm UTC](https://discourse.hibernate.org/t/hibernate-onetoone-with-mapsid-does-not-map-the-same-id-with-the-parent-entity/2093/3 "2019-01-19T19:20:33Z")

</div>

Thanks @vlad for quick reply.

I have set the parent in line [https://github.com/pmverma/hibernate-mapsid-test/blob/master/src/test/java/com/example/demo/HibernateMapsidTestApplicationTests.java#L54](https://github.com/pmverma/hibernate-mapsid-test/blob/master/src/test/java/com/example/demo/HibernateMapsidTestApplicationTests.java#L54) which is called in `@Before` so child will already have a parent during `saveAndFlush`

And here at [https://github.com/pmverma/hibernate-mapsid-test/blob/master/src/test/java/com/example/demo/HibernateMapsidTestApplicationTests.java#L87](https://github.com/pmverma/hibernate-mapsid-test/blob/master/src/test/java/com/example/demo/HibernateMapsidTestApplicationTests.java#L87), I want to change the parent. And I assume that child will have the parent which was earlier, not the updated one.

Actually, This is happening while running the integration tests of the project so I setup a similar simple one.

---

<div class="post-metadata">

**Author:** ![pmverma](https://avatars.discourse-cdn.com/v4/letter/p/e47c2d/32.png) [@pmverma](https://discourse.hibernate.org/u/pmverma)\
**Post date:** [January 19, 2019, 7:29pm UTC](https://discourse.hibernate.org/t/hibernate-onetoone-with-mapsid-does-not-map-the-same-id-with-the-parent-entity/2093/4 "2019-01-19T19:29:08Z")

</div>

Here is the request/response to update child with new parent.

> MockHttpServletRequest:  
> HTTP Method = PUT  
> Request URI = /update-child  
> Parameters = {}  
> Headers = [Content-Type:“application/json;charset=UTF-8”]  
> Body = {“id”:1,“parent”:{“id”:2}}  
> Session Attrs = {}
> 
> Handler:  
> Type = com.example.demo.MyResource  
> Method = public com.example.demo.Child com.example.demo.MyResource.updateChild(com.example.demo.Child)
> 
> Async:  
> Async started = false  
> Async result = null
> 
> Resolved Exception:  
> Type = null
> 
> ModelAndView:  
> View name = null  
> View = null  
> Model = null
> 
> FlashMap:  
> Attributes = null
> 
> MockHttpServletResponse:  
> Status = 200  
> Error message = null  
> Headers = [Content-Type:“application/json;charset=UTF-8”]  
> Content type = application/json;charset=UTF-8  
> Body = {“id”:1,“parent”:{“id”:2}}  
> Forwarded URL = null  
> Redirected URL = null  
> Cookies =

And is response, we are getting the updated parent.

---

<div class="post-metadata">

**Author:** ![vlad](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/vlad/32/658_2.png) [@vlad](https://discourse.hibernate.org/u/vlad)\
**Post date:** [January 19, 2019, 8:12pm UTC](https://discourse.hibernate.org/t/hibernate-onetoone-with-mapsid-does-not-map-the-same-id-with-the-parent-entity/2093/5 "2019-01-19T20:12:05Z")

</div>

You cannot update the parent was the child was persisted. That would mean to update the PK which is not allowed.

---

<div class="post-metadata">

**Author:** ![pmverma](https://avatars.discourse-cdn.com/v4/letter/p/e47c2d/32.png) [@pmverma](https://discourse.hibernate.org/u/pmverma)\
**Post date:** [January 19, 2019, 9:26pm UTC](https://discourse.hibernate.org/t/hibernate-onetoone-with-mapsid-does-not-map-the-same-id-with-the-parent-entity/2093/6 "2019-01-19T21:26:32Z")

</div>

I understand that updating PK is now allowed. But given that the child can set its parent to new, and this results in a new parent for the whole current transaction, how can this should be avoided?

I have done a PR to demonstrate this case at [https://github.com/vladmihalcea/high-performance-java-persistence/pull/36](https://github.com/vladmihalcea/high-performance-java-persistence/pull/36)

Regards,  
Mohan

---

<div class="post-metadata">

**Author:** ![vlad](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/vlad/32/658_2.png) [@vlad](https://discourse.hibernate.org/u/vlad)\
**Post date:** [January 20, 2019, 4:06am UTC](https://discourse.hibernate.org/t/hibernate-onetoone-with-mapsid-does-not-map-the-same-id-with-the-parent-entity/2093/7 "2019-01-20T04:06:27Z")

</div>

A shared PK requires the parent to exist. If you want to change the parent, that’s not a one-to-one association, that’s a many-to-one with a unique constraint, meaning you have to remove @MapsId.

---

<div class="post-metadata">

**Author:** ![pmverma](https://avatars.discourse-cdn.com/v4/letter/p/e47c2d/32.png) [@pmverma](https://discourse.hibernate.org/u/pmverma)\
**Post date:** [January 21, 2019, 6:00pm UTC](https://discourse.hibernate.org/t/hibernate-onetoone-with-mapsid-does-not-map-the-same-id-with-the-parent-entity/2093/8 "2019-01-21T18:00:05Z")

</div>

Thanks @vlad, I have one question regarding following.

> [@vlad](#):
>
> You cannot update the parent was the child was persisted. That would mean to update the PK which is not allowed.

I understand that changing parent is not allowed since PK updating is not allowed. But hibernate does not seems to be aware of the new parent and it is doing `em.merge(child)` successfully and returning child object with new parent. And this will lead to some subsequent un-intended errors regarding relationships.

I am contributing to add support for `@OneToOne` relationship with `@MapsId` in JHipster but I am afraid to continue more because of this issue. Is there any way Hibernate can complain or throws exception during `em.merge(child)` or anytime because it does not make sense to allow to merge the child with new parent? How can this kind of situation be prevented?

> <https://github.com/jhipster/generator-jhipster/issues/9100>
>
> \<!--
> \- Please follow the issue template below for bug reports.
> \- If you have a… support request rather than a bug, please use \[Stack Overflow\](http://stackoverflow.com/questions/tagged/jhipster) with the JHipster tag.
> \- For bug reports it is mandatory to run the command \`jhipster info\` in your project's root folder, and paste the result here.
> \- Tickets opened without any of these pieces of information will be \*\*closed\*\* without any explanation.
> \--\>
> 
> \##### \*\*Overview of the issue\*\*
> I did a PR for adding \`@MapsId\` support in #8685 . The related raised issue is #7060.
> 
> However, while writing automated tests #9094 I come to know that if you update the child entity with a new parent then this updated new parent will be used for current transaction. 
> However, in next request, next time if you load the child then it will refer to its parent which was originally mapped.
> 
> 
> 
> \##### \*\*Motivation for or Use Case\*\*
> Child should always refers to its correct parent in any case.
> 
> 
> 
> \##### \*\*Reproduce the error\*\*
> 
> 1. Generate a new app with current master
> 
> 1. Add following entity.json inside \`.jhipster\` and regenerate entity with \`jhipster entity Bar\`
> 
> \`\`\`
> {
> "fluentMethods": true,
> "clientRootFolder": "",
> "relationships": \[
> {
> "relationshipName": "user",
> "otherEntityName": "user",
> "relationshipType": "one-to-one",
> "otherEntityField": "login",
> "ownerSide": true,
> "useJPADerivedIdentifier": true,
> "otherEntityRelationshipName": "bar"
> }
> \],
> "fields": \[
> {
> "fieldName": "ddd",
> "fieldType": "String"
> }
> \],
> "changelogDate": "20181231104458",
> "dto": "no",
> "searchEngine": false,
> "service": "serviceImpl",
> "entityTableName": "bar",
> "databaseType": "sql",
> "jpaMetamodelFiltering": false,
> "pagination": "no"
> }
> 
> \`\`\`
> 
> 1. Launch the application
> 
> 1. Create a new Bar using browser and set user \`user\`
> 
> 1. Open devtool and go to network tab
> 
> 1. Update the Bar to set \`admin\`
> 
> 1. Check \`PUT\` method response body in devtool and u can see the returned bar contains \`admin\`
> 
> 1. But any new next request such as listing/view/edit will load the correct \`user\`
> 
> 
> 
> \##### \*\*Related issues\*\*
> 
> \#7060 
> \#8685 
> 
> \#9094 WIP
> 
> 
> \##### \*\*Suggest a Fix\*\*
> \- 
> \<!-- If you can't fix the bug yourself, perhaps you can point to what might be
> causing the problem (line of code or commit) --\>
> 
> \##### \*\*JHipster Version(s)\*\*
> 
> \- master branch
> \<!--
> Which version of JHipster are you using, is it a regression?
> \--\>
> 
> \##### \*\*JHipster Version(s)\*\*
> 
> \`\`\`
> mo@0.0.0 /Users/sierra/github/mp
> └── (empty)
> 
> \`\`\`
> 
> 
> \##### \*\*JHipster configuration, a \`.yo-rc.json\` file generated in the root folder\*\*
> 
> 
> \<details\>
> \<summary\>.yo-rc.json file\</summary\>
> \<pre\>
> {
> "generator-jhipster": {
> "promptValues": {
> "packageName": "com.mycompany.myapp"
> },
> "jhipsterVersion": "5.7.2",
> "applicationType": "monolith",
> "baseName": "mo",
> "packageName": "com.mycompany.myapp",
> "packageFolder": "com/mycompany/myapp",
> "serverPort": "8080",
> "authenticationType": "jwt",
> "cacheProvider": "no",
> "websocket": false,
> "databaseType": "sql",
> "devDatabaseType": "postgresql",
> "prodDatabaseType": "postgresql",
> "searchEngine": false,
> "messageBroker": false,
> "serviceDiscoveryType": false,
> "buildTool": "maven",
> "enableSwaggerCodegen": false,
> "jwtSecretKey": "bXktc2VjcmV0LXRva2VuLXRvLWNoYW5nZS1pbi1wcm9kdWN0aW9uLWFuZC10by1rZWVwLWluLWEtc2VjdXJlLXBsYWNl",
> "clientFramework": "angularX",
> "useSass": false,
> "clientPackageManager": "npm",
> "testFrameworks": \[\],
> "jhiPrefix": "jhi",
> "enableTranslation": false,
> "enableHibernateCache": false,
> "otherModules": \[\]
> }
> }
> \</pre\>
> \</details\>
> 
> 
> \##### \*\*JDL for the Entity configuration(s) \`entityName.json\` files generated in the \`.jhipster\` directory\*\*
> 
> \<details\>
> \<summary\>JDL entity definitions\</summary\>
> 
> \<pre\>
> entity Bar {
> ddd String
> }
> relationship OneToOne {
> Bar{user(login)} to User
> }
> 
> service Bar with serviceImpl
> 
> \</pre\>
> \</details\>
> 
> 
> \##### \*\*Environment and Tools\*\*
> 
> java version "1.8.0\_172"
> Java(TM) SE Runtime Environment (build 1.8.0\_172-b11)
> Java HotSpot(TM) 64-Bit Server VM (build 25.172-b11, mixed mode)
> 
> git version 2.17.2 (Apple Git-113)
> 
> node: v10.14.2
> 
> npm: 6.4.1
> 
> yeoman: 2.0.5
> 
> Docker version 18.09.0, build 4d60db4
> 
> docker-compose version 1.23.2, build 1110ad01
> 
> \- \[\] Checking this box is mandatory (this is just to show you read everything)
> 
> \<!-- Love JHipster? Please consider supporting our collective:
> 👉 https://opencollective.com/generator-jhipster/donate --\>

Regards,  
Mohan

---

<div class="post-metadata">

**Author:** ![vlad](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/vlad/32/658_2.png) [@vlad](https://discourse.hibernate.org/u/vlad)\
**Post date:** [January 21, 2019, 6:14pm UTC](https://discourse.hibernate.org/t/hibernate-onetoone-with-mapsid-does-not-map-the-same-id-with-the-parent-entity/2093/9 "2019-01-21T18:14:34Z")

</div>

First, I need to see a real-life example where this use case makes sense. A one-to-one table relationship sjpuld not sllow this. If you want to change the parent, then the child entity should have its own id.

Probably an exception is worth throwing. You should create a Jira issue and assign it to me. Post the Jira issue link here when you create it.

---

<div class="post-metadata">

**Author:** ![pmverma](https://avatars.discourse-cdn.com/v4/letter/p/e47c2d/32.png) [@pmverma](https://discourse.hibernate.org/u/pmverma)\
**Post date:** [January 24, 2019, 10:13am UTC](https://discourse.hibernate.org/t/hibernate-onetoone-with-mapsid-does-not-map-the-same-id-with-the-parent-entity/2093/10 "2019-01-24T10:13:55Z")

</div>

Hi @vlad, I have created JIRA issue for this.

[https://hibernate.atlassian.net/browse/HHH-13228](https://hibernate.atlassian.net/browse/HHH-13228)

Regards,  
Mohan

---

<div class="post-metadata">

**Author:** ![vlad](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/vlad/32/658_2.png) [@vlad](https://discourse.hibernate.org/u/vlad)\
**Post date:** [January 24, 2019, 1:24pm UTC](https://discourse.hibernate.org/t/hibernate-onetoone-with-mapsid-does-not-map-the-same-id-with-the-parent-entity/2093/11 "2019-01-24T13:24:48Z")

</div>

Thanks. I created this [Pull Request](https://github.com/hibernate/hibernate-orm/pull/2755) to log a message which tells you that the property is not going to be updated even if it was modified.

---

<div class="post-metadata">

**Author:** ![pmverma](https://avatars.discourse-cdn.com/v4/letter/p/e47c2d/32.png) [@pmverma](https://discourse.hibernate.org/u/pmverma)\
**Post date:** [January 24, 2019, 4:05pm UTC](https://discourse.hibernate.org/t/hibernate-onetoone-with-mapsid-does-not-map-the-same-id-with-the-parent-entity/2093/12 "2019-01-24T16:05:10Z")

</div>

Awesome @vlad. Thanks a lot. Is this going to be included in next release?

---

<div class="post-metadata">

**Author:** ![vlad](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/vlad/32/658_2.png) [@vlad](https://discourse.hibernate.org/u/vlad)\
**Post date:** [January 24, 2019, 4:28pm UTC](https://discourse.hibernate.org/t/hibernate-onetoone-with-mapsid-does-not-map-the-same-id-with-the-parent-entity/2093/13 "2019-01-24T16:28:33Z")

</div>

Most likely since I don’t see why it would get rejected.
