Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spring-Data @Version-Property inspection does not work with primitives for Entity State Detection #3798

Closed
Caffadras opened this issue Mar 3, 2025 · 1 comment
Assignees
Labels
type: documentation A documentation update

Comments

@Caffadras
Copy link

From the official docs:

If a property annotated with @Version is present and null, or in case of a version property of primitive type 0 the entity is considered new. If the version property is present but has a different value, the entity is considered to not be new. From my understanding, spring will use even primitive verion field to check if entity is new or not.

However, in my testing, if I create a new entity with a primitive version field, manually set Id (business requirements to set it manually), and try to save (JpaRepository.save()), spring still calls em.merge() (and not simply em.persist()), which causes an additional select query.
Changing the type to Integer resolves the issue.

Sample entity:

//...other imports
import javax.persistence.Version;

@Entity
@Table(name = "test")
public class Test implements Serializable {
    @Serial
    private static final long serialVersionUID = 1L;

    @Id
    private Long id;

    @Version
    @NotNull
    @Column(name = "entity_version")
    private int entityVersion;
}
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Mar 3, 2025
@mp911de mp911de self-assigned this Mar 3, 2025
@mp911de mp911de added type: documentation A documentation update and removed status: waiting-for-triage An issue we've not yet triaged labels Mar 3, 2025
@mp911de
Copy link
Member

mp911de commented Mar 3, 2025

The referenced fragment points to the commons part of the documentation with limited applicability. Is-New detection for all modules but JPA works that way, assuming that the initial value of a primitive version is zero and zero indicates a state before it has been inserted into the database. Once inserted in the database, the value is one.

However, with JPA, we're building on top of Hibernate and we have to align with Hibernates mechanism that considers zero as first version number and so we cannot use primitive version columns to detect the is-new state.

The Spring Data JPA Entity-State detection uses a slightly different wording at https://docs.spring.io/spring-data/jpa/reference/jpa/entity-persistence.html#jpa.entity-persistence.saving-entities.strategies, however, it doesn't point out that primitive version properties are not considered so I'm going to update the docs.

@mp911de mp911de closed this as completed in d0a54c2 Mar 3, 2025
mp911de added a commit that referenced this issue Mar 3, 2025
mp911de added a commit that referenced this issue Mar 3, 2025
@mp911de mp911de added this to the 3.3.10 (2024.0.10) milestone Mar 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: documentation A documentation update
Projects
None yet
Development

No branches or pull requests

3 participants