spring-boot: save new entity with non-empty id fail in spring boot 3.1.3

Our entity classes extend from a base resourse class with @Id and @Version, like this:

@MappedSuperclass
public class RR {

    private String id;
    private Integer jpaVersion;

    @Id
    @GeneratedValue(generator = "uuid")
    @GenericGenerator(name = "uuid", strategy = "Our_Uuid_Generator")
    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    @Version
    public Integer getJpaVersion() {
        return jpaVersion;
    }

    public void setJpaVersion(Integer jpaVersion) {
        this.jpaVersion = jpaVersion;
    }
}

In some situations we need to create a new entity and call setId() with non-empty value, and save to DB with JpaRepository.save(). This works fine up to spring boot version 3.1.2. But in spring boot 3.1.3 it seems to treat the entity with non-empty id as an existing entity and fail to save it:

org.springframework.dao.DataIntegrityViolationException: Detached entity with generated id '......' has an uninitialized version value 'null' : ......User.jpa_version
        at org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:286)
        at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:229)
        at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:550)
        at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:61)
        at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:242)
        at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:152)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:184)
        at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:164)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:184)
        at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:97)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:184)
        at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:244)
        at jdk.proxy2/jdk.proxy2.$Proxy178.save(Unknown Source)
        at our-code.createIfNotExists()

Caused by: org.hibernate.PropertyValueException: Detached entity with generated id '......' has an uninitialized version value 'null' : ......User.jpa_version
        at org.hibernate.persister.entity.AbstractEntityPersister.isTransient(AbstractEntityPersister.java:3889)
        at org.hibernate.engine.internal.ForeignKeys.isTransient(ForeignKeys.java:294)
        at org.hibernate.event.internal.EntityState.getEntityState(EntityState.java:62)
        at org.hibernate.event.internal.DefaultPersistEventListener.entityState(DefaultPersistEventListener.java:112)
        at org.hibernate.event.internal.DefaultPersistEventListener.persist(DefaultPersistEventListener.java:85)
        at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:77)
        at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:54)
        at org.hibernate.event.service.internal.EventListenerGroupImpl.fireEventOnEachListener(EventListenerGroupImpl.java:127)
        at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:755)
        at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:739)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.base/java.lang.reflect.Method.invoke(Method.java:568)
        at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:360)
        at jdk.proxy2/jdk.proxy2.$Proxy174.persist(Unknown Source)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.base/java.lang.reflect.Method.invoke(Method.java:568)
        at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:311)
        at jdk.proxy2/jdk.proxy2.$Proxy174.persist(Unknown Source)
        at org.springframework.data.jpa.repository.support.SimpleJpaRepository.save(SimpleJpaRepository.java:617)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.base/java.lang.reflect.Method.invoke(Method.java:568)
        at org.springframework.data.repository.core.support.RepositoryMethodInvoker$RepositoryFragmentMethodInvoker.lambda$new$0(RepositoryMethodInvoker.java:288)
        at org.springframework.data.repository.core.support.RepositoryMethodInvoker.doInvoke(RepositoryMethodInvoker.java:136)
        at org.springframework.data.repository.core.support.RepositoryMethodInvoker.invoke(RepositoryMethodInvoker.java:120)
        at org.springframework.data.repository.core.support.RepositoryComposition$RepositoryFragments.invoke(RepositoryComposition.java:516)
        at org.springframework.data.repository.core.support.RepositoryComposition.invoke(RepositoryComposition.java:285)
        at org.springframework.data.repository.core.support.RepositoryFactorySupport$ImplementationMethodExecutionInterceptor.invoke(RepositoryFactorySupport.java:628)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:184)
        at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.doInvoke(QueryExecutorMethodInterceptor.java:168)
        at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.invoke(QueryExecutorMethodInterceptor.java:143)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:184)
        at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:72)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:184)
        at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:123)
        at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:391)
        at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:119)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:184)
        at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:137)
        ... 175 common frames omitted

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 2
  • Comments: 19 (10 by maintainers)

Most upvoted comments

Is there any solution? What if I want to upsert entity with ID not null and version null? This should be possible. In such case I don’t want ID to be generated by generator.

Finally I found the solution.

I overwrite the new method in my custom IdentifierGenerator

@Override
public boolean allowAssignedIdentifiers() {
    return true;
}

And this allows me to save entities with preassigned id field.

What if I want to upsert entity with ID not null and version null?

Update entity without checking version will bypass optimistic lock, why would you do that?

I would expect to insert new entity.

Same issue here.

But if you’re using TestEntity in a Resource @Put , so coming from the frontend, you need the setter, right? For example when you modify the ‘name’ in an input field in a react frontend, and send it over in a put request (including the id field).

I mean remove setId() call, leave id empty for creation.

It’s introduced by hibernate/hibernate-orm@ebfaf1c https://hibernate.atlassian.net/browse/HHH-16586 Your ID generator is not assigned but you are call setId manually, I’m not sure such use case is valid, you could try setJpaVersion(0).

The document seem to suggest such case is allowed: https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#jpa.entity-persistence.saving-entites “Version-Property and Id-Property inspection (default): By default Spring Data JPA inspects first if there is a Version-property of non-primitive type. If there is, the entity is considered new if the value of that property is null. Without such a Version-property Spring Data JPA inspects the identifier property of the given entity. If the identifier property is null, then the entity is assumed to be new. Otherwise, it is assumed to be not new.”

First this is the state-detection Strategy from spring-data-jpa, not official JPA Spec, hibernate could act in different behavior. Second the actual arguable user case is manually assign id to entity which doesn’t have an assigned ID generator, from perspective of spring-data-jpa its state is NEW and call of repo.save() will be delegated to em.persist(), It’s correct behavior, but from perspective of hibernate, Its state is DETACHED.

org.hibernate.PropertyValueException: Detached entity with generated id ‘…’ has an uninitialized version value ‘null’ : …User.jpa_version