# After upgrading Hibernate, JpaRepository does not execute the update SQL

**URL:** <https://discourse.hibernate.org/t/after-upgrading-hibernate-jparepository-does-not-execute-the-update-sql/9513>\
**Category:** Hibernate ORM\
**Created:** [May 14, 2024, 6:20am UTC](https://discourse.hibernate.org/t/after-upgrading-hibernate-jparepository-does-not-execute-the-update-sql/9513 "2024-05-14T06:20:15Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Naomi\_Chen](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/naomi_chen/32/3202_2.png) [@Naomi\_Chen](https://discourse.hibernate.org/u/Naomi_Chen)\
**Post date:** [May 14, 2024, 6:20am UTC](https://discourse.hibernate.org/t/after-upgrading-hibernate-jparepository-does-not-execute-the-update-sql/9513/1 "2024-05-14T06:20:16Z")

</div>

When using Hibernate version 6.4.4.Final, calling the `save` method of JpaRepository did not trigger an update SQL statement, whereas it did when using version 5.4.15.Final.

Below is my code:

```auto
public interface CustomObjectRepository extends JpaRepository<CustomObject, CustomObjectPK> {

}

@Service
public class CustomObjectService {

	@Autowired
	private CustomObjectRepository customObjectRepository;

	@Transactional
	public void updateCounts(CustomObjectPK customObjectPK, long currentCounts) {
		long previousCounts = customObjectRepository.findById(customObjectPK).map(entity -> entity.getCounts())
				.orElse(0L);
		CustomObject customObject = new CustomObject();
		customObject.setCustomObjectPK(customObjectPK);
		customObject.setLastUpdatedTime(new Date());
		customObject.setCounts(previousCounts + currentCounts);
		customObjectRepository.save(customObject);
	}

}

@Getter
@Setter
@Entity
@Table(name = "CUSTOM_OBJECT")
public class CustomObject {

	@EmbeddedId
	private CustomObjectPK customObjectPK;
	@Column(name = "COUNTS", nullable = false)
	private long counts;
	@Column(name = "LAST_UPDATED_TIME")
	private Date lastUpdatedTime;

	public CustomObject() {}

	public CustomObject(CustomObjectPK customObjectPK) {
		this.customObjectPK = customObjectPK;
	}
}

@Getter
@Setter
@Embeddable
public class CustomObjectPK implements Serializable {
	private static final long serialVersionUID = -8508836212101729149L;

	@Column(name = "INSTANCE_ID", nullable = false, length = 100)
	private String instanceId;
	@Column(name = "UUID", nullable = false, length = 100)
	private String uuid;
	@Column(name = "TAG", nullable = false, length = 50)
	private String tag;
	@Column(name = "PART", nullable = false, length = 6)
	private String part;

	public CustomObjectPK() {
		super();
	}

	public CustomObjectPK(String instanceId, String uuid, String tag, String part) {
		super();
		this.instanceId = instanceId;
		this.uuid = uuid;
		this.tag = tag;
		this.part = part;
	}
}

```

When I execute the `updateCounts` method and `currentCount` is 1, and there was no data for the given PK before, the `save` method can execute an insert SQL statement. However, if there was data for the PK before and `previousCount` was 1, the `save` method does not execute an update SQL statement.

I understand that if all parameters remain unchanged, an update SQL will not be executed. However, since the count increased by 1, I’m wondering why an update SQL statement is not being executed in Hibernate 6.4.4.Final.

Thanks for any help!

---

<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:** [May 14, 2024, 10:51am UTC](https://discourse.hibernate.org/t/after-upgrading-hibernate-jparepository-does-not-execute-the-update-sql/9513/2 "2024-05-14T10:51:57Z")

</div>

A `merge` call does not necessarily require an immediate flush operation. Usually, the flush is deferred until a transaction ends to avoid holding locks for a long time.  
If you want to see the update statement happen immediately, use `saveAndFlush()` or whatever the Spring Data method is called. Note that this is not a Spring forum, so you usually will not find answers for such Spring questions.

---

<div class="post-metadata">

**Author:** ![Naomi\_Chen](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/naomi_chen/32/3202_2.png) [@Naomi\_Chen](https://discourse.hibernate.org/u/Naomi_Chen)\
**Post date:** [May 16, 2024, 5:42am UTC](https://discourse.hibernate.org/t/after-upgrading-hibernate-jparepository-does-not-execute-the-update-sql/9513/3 "2024-05-16T05:42:24Z")

</div>

Thank you for the response! However, even when I use the saveAndFlush method, it still does not execute the update SQL. Currently, I found that the following code can execute the update SQL correctly:

```auto
@Service
public class CustomObjectService Service {

	@Autowired
	private CustomObjectRepository customObjectRepository;

	@Transactional
	public void updateCounts(CustomObjectPK customObjectPK, long currentCounts, String protocol) {
		CustomObject customObject = customObjectRepository.findById(customObjectPK).orElse(new CustomObject(customObjectPK));
		customObject.setLastUpdatedTime(new Date());
		customObject.setCounts(customObject.getCounts() + currentCounts);
		customObject.setProtocol(protocol);

		customObjectRepository.save(customObject);
	}

}

```

However, if I add the following line of code, the save method no longer executes the update SQL:

```auto
customObject.setCustomObjectPK(customObject);

```

I tried updating data in other tables and found that only tables using @EmbeddedId fail to execute the update SQL properly after setting its primary key.

Below is my CustomObjectPK override method:

```auto
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((uuid == null) ? 0 : uuid.hashCode());
		result = prime * result + ((part == null) ? 0 : part.hashCode());
		result = prime * result + ((instanceId == null) ? 0 : instanceId.hashCode());
		result = prime * result + ((tag == null) ? 0 : tag.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		CustomObjectPK other = (CustomObjectPK) obj;
		if (uuid == null) {
			if (other.uuid != null)
				return false;
		} else if (!uuid.equals(other.uuid))
			return false;
		if (part == null) {
			if (other.part != null)
				return false;
		} else if (!part.equals(other.part))
			return false;
		if (instanceId == null) {
			if (other.instanceId != null)
				return false;
		} else if (!instanceId.equals(other.instanceId))
			return false;
		if (tag == null) {
			if (other.tag != null)
				return false;
		} else if (!tag.equals(other.tag))
			return false;
		return true;
	}

	@Override
	public String toString() {
		return ReflectionToStringBuilder.toString(this);
	}

```

---

<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:** [May 16, 2024, 8:02am UTC](https://discourse.hibernate.org/t/after-upgrading-hibernate-jparepository-does-not-execute-the-update-sql/9513/4 "2024-05-16T08:02:14Z")

</div>

If you think this is a Hibernate ORM bug, please try to create a reproducer with our [test case template](https://github.com/hibernate/hibernate-test-case-templates/blob/master/orm/hibernate-orm-6/src/test/java/org/hibernate/bugs/JPAUnitTestCase.java) and if you are able to reproduce the issue, create a bug ticket in our [issue tracker](https://hibernate.atlassian.net) and attach that reproducer.
