# Updating reference to entity, not fields of referenced entity

**URL:** <https://discourse.hibernate.org/t/updating-reference-to-entity-not-fields-of-referenced-entity/4091>\
**Category:** Hibernate ORM\
**Created:** [April 27, 2020, 10:42pm UTC](https://discourse.hibernate.org/t/updating-reference-to-entity-not-fields-of-referenced-entity/4091 "2020-04-27T22:42:17Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![alex-t0](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/alex-t0/32/1051_2.png) [@alex-t0](https://discourse.hibernate.org/u/alex-t0)\
**Post date:** [April 27, 2020, 10:42pm UTC](https://discourse.hibernate.org/t/updating-reference-to-entity-not-fields-of-referenced-entity/4091/1 "2020-04-27T22:42:17Z")

</div>

I have Document entity, and Company entity. Each document belongs to some company.

```auto
    @ManyToOne(fetch = FetchType.LAZY, cascade = { CascadeType.MERGE, CascadeType.PERSIST }, targetEntity = Organization.class)
    @JoinColumn(name="org")
    private Organization org;

```

Document can be sent to BPM engine, and can be returned from BPM engine as serialized data, than deserialzed back to entity object and persisted/merged in entity manager.

So, when I call merge method I get error:

```auto
java.lang.IllegalStateException: org.hibernate.TransientPropertyValueException: object references an unsaved transient instance - save the transient instance before flushing : my.app.models.Organization.orgType -> my.app.models.OrgType

```

It’s because company hibernate treat document as detached object and tries to save all entities with child entities. But I don’t want to save OrgType. More, I don’t want to save changes in company, because it may be a security issue - users can’t change companies or company types. But if changes companyId in document - I want to save [Document.Org](http://Document.Org) reference in db.

So, I need to implement “update only id reference, if referenced entity changes”.

What is the most simple way to do it?
