From 84a6d929c1bcb30022060b1047b695c674072f48 Mon Sep 17 00:00:00 2001 From: Christoph Strobl Date: Thu, 10 Jul 2014 13:38:51 +0200 Subject: [PATCH] DATAJPA-568 - Fix is-new detection for primitive version properties. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- .../JpaMetamodelEntityInformation.java | 5 +-- .../sample/PrimitiveVersionProperty.java | 26 ++++++++++++-- ...odelEntityInformationIntegrationTests.java | 35 +++++++++++++++++++ 3 files changed, 62 insertions(+), 4 deletions(-) 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 356340cb74..0a03c6a72e 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 @@ -43,6 +43,7 @@ * * @author Oliver Gierke * @author Thomas Darimont + * @author Christoph Strobl */ public class JpaMetamodelEntityInformation extends JpaEntityInformationSupport { @@ -192,7 +193,7 @@ public Object getCompositeIdAttributeValue(Serializable id, String idAttribute) @Override public boolean isNew(T entity) { - if (versionAttribute == null) { + if (versionAttribute == null || versionAttribute.getJavaType().isPrimitive()) { return super.isNew(entity); } @@ -203,7 +204,7 @@ public boolean isNew(T entity) { return true; } - return versionAttribute.getJavaType().isPrimitive() && ((Number) versionValue).longValue() == 0; + return ((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 index bb2ba3bb42..735be8ff16 100644 --- a/src/test/java/org/springframework/data/jpa/domain/sample/PrimitiveVersionProperty.java +++ b/src/test/java/org/springframework/data/jpa/domain/sample/PrimitiveVersionProperty.java @@ -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; } 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 d849cca29b..d9e4fad0e8 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 @@ -52,6 +52,7 @@ * * @author Oliver Gierke * @author Thomas Darimont + * @author Christoph Strobl */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration({ "classpath:infrastructure.xml" }) @@ -182,6 +183,39 @@ public void considersEntityWithPrimitiveVersionPropertySetToDefaultNew() { assertThat(information.isNew(new PrimitiveVersionProperty()), is(true)); } + /** + * @see DATAJPA-568 + */ + @Test + public void considersEntityAsNotNewWhenHavingIdSetAndUsingPrimitiveTypeForVersionProperty() { + + EntityInformation information = new JpaMetamodelEntityInformation( + 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 information = new JpaMetamodelEntityInformation( + 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 static abstract class Identifiable { public static class Sample extends Identifiable { } + }