Closed
Description
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;
}