From 146041cda3c4c4e7bafa87747fbfa2ae24d16c62 Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Thu, 26 Jun 2014 16:15:03 +0200 Subject: [PATCH] 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. --- .../support/JpaMetamodelEntityInformation.java | 9 ++++++++- .../jpa/domain/sample/PrimitiveVersionProperty.java | 12 ++++++++++++ ...aMetamodelEntityInformationIntegrationTests.java | 13 +++++++++++++ src/test/resources/META-INF/persistence.xml | 1 + 4 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 src/test/java/org/springframework/data/jpa/domain/sample/PrimitiveVersionProperty.java diff --git a/src/main/java/org/springframework/data/jpa/repository/support/JpaMetamodelEntityInformation.java b/src/main/java/org/springframework/data/jpa/repository/support/JpaMetamodelEntityInformation.java index 401941b0e..356340cb7 100644 --- a/src/main/java/org/springframework/data/jpa/repository/support/JpaMetamodelEntityInformation.java +++ b/src/main/java/org/springframework/data/jpa/repository/support/JpaMetamodelEntityInformation.java @@ -196,7 +196,14 @@ public class JpaMetamodelEntityInformation 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; } /** diff --git a/src/test/java/org/springframework/data/jpa/domain/sample/PrimitiveVersionProperty.java b/src/test/java/org/springframework/data/jpa/domain/sample/PrimitiveVersionProperty.java new file mode 100644 index 000000000..bb2ba3bb4 --- /dev/null +++ b/src/test/java/org/springframework/data/jpa/domain/sample/PrimitiveVersionProperty.java @@ -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; +} diff --git a/src/test/java/org/springframework/data/jpa/repository/support/JpaMetamodelEntityInformationIntegrationTests.java b/src/test/java/org/springframework/data/jpa/repository/support/JpaMetamodelEntityInformationIntegrationTests.java index a5d0fd2bb..d849cca29 100644 --- a/src/test/java/org/springframework/data/jpa/repository/support/JpaMetamodelEntityInformationIntegrationTests.java +++ b/src/test/java/org/springframework/data/jpa/repository/support/JpaMetamodelEntityInformationIntegrationTests.java @@ -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 information = new JpaMetamodelEntityInformation( + PrimitiveVersionProperty.class, em.getMetamodel()); + + assertThat(information.isNew(new PrimitiveVersionProperty()), is(true)); + } + protected String getMetadadataPersitenceUnitName() { return "metadata"; } diff --git a/src/test/resources/META-INF/persistence.xml b/src/test/resources/META-INF/persistence.xml index 9270c5aef..28c6dd8ff 100644 --- a/src/test/resources/META-INF/persistence.xml +++ b/src/test/resources/META-INF/persistence.xml @@ -10,6 +10,7 @@ org.springframework.data.jpa.domain.sample.AuditableUser org.springframework.data.jpa.domain.sample.AuditableRole org.springframework.data.jpa.domain.sample.Parent + org.springframework.data.jpa.domain.sample.PrimitiveVersionProperty org.springframework.data.jpa.domain.sample.Child org.springframework.data.jpa.domain.sample.Role org.springframework.data.jpa.domain.sample.SpecialUser