Browse Source

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.
1.6.x
Oliver Gierke 12 years ago
parent
commit
146041cda3
  1. 9
      src/main/java/org/springframework/data/jpa/repository/support/JpaMetamodelEntityInformation.java
  2. 12
      src/test/java/org/springframework/data/jpa/domain/sample/PrimitiveVersionProperty.java
  3. 13
      src/test/java/org/springframework/data/jpa/repository/support/JpaMetamodelEntityInformationIntegrationTests.java
  4. 1
      src/test/resources/META-INF/persistence.xml

9
src/main/java/org/springframework/data/jpa/repository/support/JpaMetamodelEntityInformation.java

@ -196,7 +196,14 @@ public class JpaMetamodelEntityInformation<T, ID extends Serializable> extends J @@ -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;
}
/**

12
src/test/java/org/springframework/data/jpa/domain/sample/PrimitiveVersionProperty.java

@ -0,0 +1,12 @@ @@ -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;
}

13
src/test/java/org/springframework/data/jpa/repository/support/JpaMetamodelEntityInformationIntegrationTests.java

@ -36,6 +36,7 @@ import org.junit.Ignore; @@ -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 { @@ -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";
}

1
src/test/resources/META-INF/persistence.xml

@ -10,6 +10,7 @@ @@ -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>

Loading…
Cancel
Save