# Mapping Help of OneToOne with Lazy Loading and Single call to save

**URL:** <https://discourse.hibernate.org/t/mapping-help-of-onetoone-with-lazy-loading-and-single-call-to-save/148>\
**Category:** Hibernate ORM\
**Created:** [January 27, 2018, 6:18pm UTC](https://discourse.hibernate.org/t/mapping-help-of-onetoone-with-lazy-loading-and-single-call-to-save/148 "2018-01-27T18:18:21Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![imohitsaxena](https://avatars.discourse-cdn.com/v4/letter/i/65b543/32.png) [@imohitsaxena](https://discourse.hibernate.org/u/imohitsaxena)\
**Post date:** [January 27, 2018, 6:18pm UTC](https://discourse.hibernate.org/t/mapping-help-of-onetoone-with-lazy-loading-and-single-call-to-save/148/1 "2018-01-27T18:18:21Z")

</div>

Here is the details of problem statement:

Entities:  
Employee  
Address  
 ![Relations](https://canada1.discourse-cdn.com/flex035/uploads/hibernate/original/1X/027bfec31dff35cbb2ba2b4a47c9a2d28b291486.png)

Now, what I need is that:

1. There should be OneToOne Mapping between Entities.
2. Primary Key (and Foreign Key) are manually generated. I don’t this automatically generated.
3. I create Employee and Address object, setup required fields and set just address in employee (not employee in address) employee.setAddress(address).
4. When persisting employee object, it should save employee, and address and setup FK relation in Address table with Employee. I don’t need to save Address separately.
5. Next, while retrieving data, I would like when getting Employee, Address should be lazily loaded.
6. Also, another important point is Address field is optional wrt Employee. I mean, Every address will have exactly one Employee, but not all Employee will have address.

---

<div class="post-metadata">

**Author:** ![imohitsaxena](https://avatars.discourse-cdn.com/v4/letter/i/65b543/32.png) [@imohitsaxena](https://discourse.hibernate.org/u/imohitsaxena)\
**Post date:** [January 27, 2018, 6:34pm UTC](https://discourse.hibernate.org/t/mapping-help-of-onetoone-with-lazy-loading-and-single-call-to-save/148/2 "2018-01-27T18:34:25Z")

</div>

Okay I figured out, most of the things. My only problem remains is that I need to set Employee to address and address to employee. Only then both persist on persisting employee.  
If I don’t set address.setEmployee(employee);  
I get attempted to assign id from null one-to-one property [com.mohit.learn.entity.Address.employee]

---

<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 28, 2018, 6:48am UTC](https://discourse.hibernate.org/t/mapping-help-of-onetoone-with-lazy-loading-and-single-call-to-save/148/3 "2018-01-28T06:48:00Z")

</div>

Yes, you need to set both sides if this is a bidirectional association and you rely on cascade.

---

<div class="post-metadata">

**Author:** ![imohitsaxena](https://avatars.discourse-cdn.com/v4/letter/i/65b543/32.png) [@imohitsaxena](https://discourse.hibernate.org/u/imohitsaxena)\
**Post date:** [February 1, 2018, 2:35pm UTC](https://discourse.hibernate.org/t/mapping-help-of-onetoone-with-lazy-loading-and-single-call-to-save/148/4 "2018-02-01T14:35:37Z")

</div>

Thanks Vlad! Finally figured out solution.

---

<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:** [February 1, 2018, 2:50pm UTC](https://discourse.hibernate.org/t/mapping-help-of-onetoone-with-lazy-loading-and-single-call-to-save/148/5 "2018-02-01T14:50:21Z")

</div>

You are very welcome.
