DATAJPA-561 - JpaMetamodelEntityInformation now treats primitive version properties correctly.
Previously, we only checked the version attribute's value against null, which effectively invalidly reported a new entity being non-new in case it used primitive types for the version attribute (as it has a default value then). We now explicitly check for primitive version property types and consider default values to indicate new state.
This commit is contained in:
@@ -196,7 +196,14 @@ public class JpaMetamodelEntityInformation<T, ID extends Serializable> extends J
|
||||
return super.isNew(entity);
|
||||
}
|
||||
|
||||
return new DirectFieldAccessFallbackBeanWrapper(entity).getPropertyValue(versionAttribute.getName()) == null;
|
||||
BeanWrapper wrapper = new DirectFieldAccessFallbackBeanWrapper(entity);
|
||||
Object versionValue = wrapper.getPropertyValue(versionAttribute.getName());
|
||||
|
||||
if (versionValue == null) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return versionAttribute.getJavaType().isPrimitive() && ((Number) versionValue).longValue() == 0;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
package org.springframework.data.jpa.domain.sample;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Version;
|
||||
|
||||
@Entity
|
||||
public class PrimitiveVersionProperty {
|
||||
|
||||
@Id Long id;
|
||||
@Version long version;
|
||||
}
|
||||
@@ -36,6 +36,7 @@ import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.data.jpa.domain.AbstractPersistable;
|
||||
import org.springframework.data.jpa.domain.sample.PrimitiveVersionProperty;
|
||||
import org.springframework.data.jpa.domain.sample.Role;
|
||||
import org.springframework.data.jpa.domain.sample.SampleWithIdClass;
|
||||
import org.springframework.data.jpa.domain.sample.SampleWithIdClassPK;
|
||||
@@ -169,6 +170,18 @@ public class JpaMetamodelEntityInformationIntegrationTests {
|
||||
assertThat(info.getEntityName(), is("ROLE"));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAJPA-561
|
||||
*/
|
||||
@Test
|
||||
public void considersEntityWithPrimitiveVersionPropertySetToDefaultNew() {
|
||||
|
||||
EntityInformation<PrimitiveVersionProperty, Serializable> information = new JpaMetamodelEntityInformation<PrimitiveVersionProperty, Serializable>(
|
||||
PrimitiveVersionProperty.class, em.getMetamodel());
|
||||
|
||||
assertThat(information.isNew(new PrimitiveVersionProperty()), is(true));
|
||||
}
|
||||
|
||||
protected String getMetadadataPersitenceUnitName() {
|
||||
return "metadata";
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
<class>org.springframework.data.jpa.domain.sample.AuditableUser</class>
|
||||
<class>org.springframework.data.jpa.domain.sample.AuditableRole</class>
|
||||
<class>org.springframework.data.jpa.domain.sample.Parent</class>
|
||||
<class>org.springframework.data.jpa.domain.sample.PrimitiveVersionProperty</class>
|
||||
<class>org.springframework.data.jpa.domain.sample.Child</class>
|
||||
<class>org.springframework.data.jpa.domain.sample.Role</class>
|
||||
<class>org.springframework.data.jpa.domain.sample.SpecialUser</class>
|
||||
|
||||
Reference in New Issue
Block a user