# Unexpected @Version auto-increment

**URL:** <https://discourse.hibernate.org/t/unexpected-version-auto-increment/12195>\
**Category:** Hibernate ORM\
**Created:** [March 29, 2026, 3:40pm UTC](https://discourse.hibernate.org/t/unexpected-version-auto-increment/12195 "2026-03-29T15:40:13Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![horvoje](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/horvoje/32/779_2.png) [@horvoje](https://discourse.hibernate.org/u/horvoje)\
**Post date:** [March 29, 2026, 3:40pm UTC](https://discourse.hibernate.org/t/unexpected-version-auto-increment/12195/1 "2026-03-29T15:40:13Z")

</div>

Hi!

I’m trying to implement optimistic locking using `@jakarta.persistence.Version`  
and I got weird behaviour: `version` is incremented 2 times from the time of  
fetching until the time right after the entity is persisted with `save()`.  
Each product (`Article`) has its own so called `TextID` which is used to build final URL; e.g., “Gluten-free pasta 500 g” gives “gluten-free-pasta-500-g”. Because more than one product can have the same name, I have to check for duplicates, and if previously added pasta is found (e.g., different manufacturer), the suffix is added to `TextID` (“-1”) and it becomes “gluten-free-pasta-500-g-1”.  
To achieve that, I have to issue a search accross existing products, and that causes `Article` version to increment.  
Why does entity search/load trigger version incremenataion?  
What am I doing wrong here?

* * *

At the beginning I had the same problem with the store that sells the product (`Provider`) because each price has its `Provider` attached (`Article` → `PriceInfo` → `Provider`).  
I fixed that using entity manager although I prefer Hibernate search as everything else is fetched with it:

```java
@PersistenceContext
private EntityManager entityManager;

// ...

final Provider provider =
    this.entityManager.getReference( priceInfoDTO.getProviderDTO().getId() );

```

The case with `TextID` cannot be fixed this way because I have to filter by `TextID` value and not by entity `id` or any other `id`.

* * *

Technical:

- Java 21
- Hibernate 7.3.0.Final
- Hibernate Search 8.3.0.Final

* * *

```java
public abstract class BaseEntity implements Serializable {
	// ...

	@jakarta.persistence.Version
	@Column( nullable = false )
	private int version;

	// ...
}

```

```java
@Entity
class Article extends BaseEntity {
	// ...

	@OneToMany( mappedBy = "article", fetch = FetchType.LAZY )
	@IndexedEmbedded @JsonManagedReference
	@AssociationInverseSide(
		inversePath = @ObjectPath(
			@PropertyValue( propertyName = "priceInfo" )
		)
	)
	private Set<PriceInfo> priceInfos = new HashSet<>();

	// ...
}

```

```java
@Entity
class PriceInfo extends BaseEntity {
	// ...

	@Reference
	@JsonBackReference
	@ManyToOne( fetch = FetchType.LAZY )
	@IndexedEmbedded( includePaths = {
		"id", "name", "shortName", "description", "searchAid", "tags.name",
		"tags.searchAid", "barcodes", "manufacturer.name",
		"manufacturer.shortName", "manufacturer.description",
		"manufacturer.searchAid"
	} )
	private Article article;

	@Reference @JsonBackReference
	@ManyToOne @IndexedEmbedded
	private Provider provider;

	// ...
}

```

---

<div class="post-metadata">

**Author:** ![horvoje](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/horvoje/32/779_2.png) [@horvoje](https://discourse.hibernate.org/u/horvoje)\
**Post date:** [March 30, 2026, 9:25am UTC](https://discourse.hibernate.org/t/unexpected-version-auto-increment/12195/2 "2026-03-30T09:25:29Z")

</div>

I found a way how to avoid that behaviour without knowing the cause of it:  
One has to move any Hibernate Search of dependent or any other entities above fetching versioned one.
