# .saveOrUpdate() issues sql statements but data is missing in table

**URL:** <https://discourse.hibernate.org/t/saveorupdate-issues-sql-statements-but-data-is-missing-in-table/6887>\
**Category:** Hibernate ORM\
**Created:** [November 11, 2022, 7:36am UTC](https://discourse.hibernate.org/t/saveorupdate-issues-sql-statements-but-data-is-missing-in-table/6887 "2022-11-11T07:36:40Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![d00d](https://avatars.discourse-cdn.com/v4/letter/d/49beb7/32.png) [@d00d](https://discourse.hibernate.org/u/d00d)\
**Post date:** [November 11, 2022, 7:36am UTC](https://discourse.hibernate.org/t/saveorupdate-issues-sql-statements-but-data-is-missing-in-table/6887/1 "2022-11-11T07:36:40Z")

</div>

Edit: After some more debugging, I have changed the description of this question

In Hibernate 5.6 with Spring 5.3 I have very weird issue. I want to insert a new user role into a table. i can see the insert-statement in console log but the data never appears in the table.

The table:

```auto
CREATE TABLE public.ums_t_user_role (
	fk_role_id int8 NOT NULL,
	fk_user_id varchar(500) NOT NULL,
	CONSTRAINT user_role_combine_key PRIMARY KEY (fk_role_id, fk_user_id)
);

```

The .hbm.xml mapping:

```auto
<hibernate-mapping>
    <class name="com.myapp.ums.business.impl.UmsUserRoleVOImpl" table="ums_t_user_role"
           proxy="com.myapp.ums.common.business.UmsUserRoleVO">
        <composite-id>
            <key-property name="userName" type="java.lang.String" column="fk_user_id"/>
            <key-property name="roleName" type="java.lang.Long" column="fk_role_id"/>
        </composite-id>
    </class>
</hibernate-mapping>

```

The business logic:

```auto
UmsUserRoleVO userRole = new UmsUserRoleVOImpl();
userRole.setUserName(userVO.getLoginId());
userRole.setRoleName(Long.valueOf(role));
this.userRoleDAO.save(userRole);

```

The userRoleDAO:

```auto
@Override
public final void save(final ENTITY instance) {
    	getHibernateTemplate().saveOrUpdate(instance);
    	getHibernateTemplate().flush();
}

```

When I hit the userRole.DAO method, I can see that the select + insert statements are generated in console. But the data is missing in the table. Any idea what the issue could be here?

```auto
											Hibernate:
13:05:33,583 INFO [stdout] (default task-7) select
13:05:33,583 INFO [stdout] (default task-7) umsuserrol_.fk_user_id,
13:05:33,583 INFO [stdout] (default task-7) umsuserrol_.fk_role_id
13:05:33,583 INFO [stdout] (default task-7) from
13:05:33,583 INFO [stdout] (default task-7) ums_t_user_role umsuserrol_
13:05:33,583 INFO [stdout] (default task-7) where
13:05:33,583 INFO [stdout] (default task-7) umsuserrol_.fk_user_id=?
13:05:33,583 INFO [stdout] (default task-7) and umsuserrol_.fk_role_id=?
13:05:33,583 INFO [stdout] (default task-7) Hibernate:
13:05:33,583 INFO [stdout] (default task-7) insert
13:05:33,583 INFO [stdout] (default task-7) into
13:05:33,583 INFO [stdout] (default task-7) ums_t_user_role
13:05:33,583 INFO [stdout] (default task-7) (fk_user_id, fk_role_id)
13:05:33,583 INFO [stdout] (default task-7) values
13:05:33,583 INFO [stdout] (default task-7) (?, ?)

```

---

<div class="post-metadata">

**Author:** ![beikov](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/beikov/32/258_2.png) [@beikov](https://discourse.hibernate.org/u/beikov)\
**Post date:** [November 14, 2022, 9:30am UTC](https://discourse.hibernate.org/t/saveorupdate-issues-sql-statements-but-data-is-missing-in-table/6887/2 "2022-11-14T09:30:34Z")

</div>

Did you commit the transaction?

---

<div class="post-metadata">

**Author:** ![d00d](https://avatars.discourse-cdn.com/v4/letter/d/49beb7/32.png) [@d00d](https://discourse.hibernate.org/u/d00d)\
**Post date:** [November 14, 2022, 3:21pm UTC](https://discourse.hibernate.org/t/saveorupdate-issues-sql-statements-but-data-is-missing-in-table/6887/3 "2022-11-14T15:21:57Z")

</div>

This turned out to be a non-hibernate issue. I can see in the Postgres logs that the insert command was issued as it should but data remains missing. I created a clone of the table with the same structure but no data and added this new table to my .hbm.xml file and the data was written properly. Switched back to the old table - data not added. Will continue evaluation on DB level.

---

<div class="post-metadata">

**Author:** ![d00d](https://avatars.discourse-cdn.com/v4/letter/d/49beb7/32.png) [@d00d](https://discourse.hibernate.org/u/d00d)\
**Post date:** [November 15, 2022, 9:17am UTC](https://discourse.hibernate.org/t/saveorupdate-issues-sql-statements-but-data-is-missing-in-table/6887/4 "2022-11-15T09:17:03Z")

</div>

OK another addendum in case anybody every runs into this problem. I finally found in the postgres logs that after the insert to user\_role table, another delete statement was issued that deleted the user role right away.

Problem was that I have three tables

user  
roles  
user\_roles

And in my User.hbm.xml I have a set that tracks the assigned roles of that user

```auto
<set name="umsRoles" table="ums_t_user_role" inverse="false" lazy="false" fetch="select">
            <key property-ref="loginId">
                <column name="fk_user_id" not-null="true" />
            </key>
            <many-to-many entity-name="com.myApp.ums.business.impl.UmsRoleVOImpl" >
                <column name="fk_role_id" not-null="true" />
            </many-to-many>
</set>

```

Problem was that we wrote the user\_role directly to user\_role table instead of just assigning that role to the umsRoles collection in UmsUserVOImpl and saving the user object. Hence, when saving the user, Hibernate wanted to keep the data consistent and removed all roles of that user again because the collection was empty.
