DATAJPA-568 - Fix is-new detection for primitive version properties.
Some persistence providers do not bump the version number in an initial call to EntityManager.persist(…). Thus, in case of a primitive version property we cannot distinguish between a new entity and one already persisted. We now fall back to id inspection whenever we find a primitive version property. Original pull request: #103.
This commit is contained in:
committed by
Oliver Gierke
parent
59176d6a0a
commit
68ed8a9082
@@ -43,6 +43,7 @@ import org.springframework.util.Assert;
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
* @author Thomas Darimont
|
||||
* @author Christoph Strobl
|
||||
*/
|
||||
public class JpaMetamodelEntityInformation<T, ID extends Serializable> extends JpaEntityInformationSupport<T, ID> {
|
||||
|
||||
@@ -192,7 +193,7 @@ public class JpaMetamodelEntityInformation<T, ID extends Serializable> extends J
|
||||
@Override
|
||||
public boolean isNew(T entity) {
|
||||
|
||||
if (versionAttribute == null) {
|
||||
if (versionAttribute == null || versionAttribute.getJavaType().isPrimitive()) {
|
||||
return super.isNew(entity);
|
||||
}
|
||||
|
||||
@@ -203,7 +204,7 @@ public class JpaMetamodelEntityInformation<T, ID extends Serializable> extends J
|
||||
return true;
|
||||
}
|
||||
|
||||
return versionAttribute.getJavaType().isPrimitive() && ((Number) versionValue).longValue() == 0;
|
||||
return ((Number) versionValue).longValue() == 0;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,12 +1,34 @@
|
||||
/*
|
||||
* Copyright 2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.jpa.domain.sample;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Version;
|
||||
|
||||
/**
|
||||
* @author Oliver Gierke
|
||||
* @author Christoph Strobl
|
||||
*/
|
||||
@Entity
|
||||
public class PrimitiveVersionProperty {
|
||||
|
||||
@Id Long id;
|
||||
@Version long version;
|
||||
public @Id @GeneratedValue Long id;
|
||||
public @Version long version;
|
||||
|
||||
public String someValue;
|
||||
}
|
||||
|
||||
@@ -52,6 +52,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
* @author Thomas Darimont
|
||||
* @author Christoph Strobl
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration({ "classpath:infrastructure.xml" })
|
||||
@@ -182,6 +183,39 @@ public class JpaMetamodelEntityInformationIntegrationTests {
|
||||
assertThat(information.isNew(new PrimitiveVersionProperty()), is(true));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAJPA-568
|
||||
*/
|
||||
@Test
|
||||
public void considersEntityAsNotNewWhenHavingIdSetAndUsingPrimitiveTypeForVersionProperty() {
|
||||
|
||||
EntityInformation<PrimitiveVersionProperty, Serializable> information = new JpaMetamodelEntityInformation<PrimitiveVersionProperty, Serializable>(
|
||||
PrimitiveVersionProperty.class, em.getMetamodel());
|
||||
|
||||
PrimitiveVersionProperty pvp = new PrimitiveVersionProperty();
|
||||
pvp.id = 100L;
|
||||
|
||||
assertThat(information.isNew(pvp), is(false));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAJPA-568
|
||||
*/
|
||||
@Test
|
||||
public void fallsBackToIdInspectionForAPrimitiveVersionProperty() {
|
||||
|
||||
EntityInformation<PrimitiveVersionProperty, Serializable> information = new JpaMetamodelEntityInformation<PrimitiveVersionProperty, Serializable>(
|
||||
PrimitiveVersionProperty.class, em.getMetamodel());
|
||||
|
||||
PrimitiveVersionProperty pvp = new PrimitiveVersionProperty();
|
||||
pvp.version = 1L;
|
||||
|
||||
assertThat(information.isNew(pvp), is(true));
|
||||
|
||||
pvp.id = 1L;
|
||||
assertThat(information.isNew(pvp), is(false));
|
||||
}
|
||||
|
||||
protected String getMetadadataPersitenceUnitName() {
|
||||
return "metadata";
|
||||
}
|
||||
@@ -207,4 +241,5 @@ public class JpaMetamodelEntityInformationIntegrationTests {
|
||||
public static class Sample extends Identifiable {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user