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 356340cb7..0a03c6a72 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 @@ import org.springframework.util.Assert; * * @author Oliver Gierke * @author Thomas Darimont + * @author Christoph Strobl */ public class JpaMetamodelEntityInformation extends JpaEntityInformationSupport { @@ -192,7 +193,7 @@ public class JpaMetamodelEntityInformation 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 extends J 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 bb2ba3bb4..735be8ff1 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 d849cca29..d9e4fad0e 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 @@ 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 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 class JpaMetamodelEntityInformationIntegrationTests { public static class Sample extends Identifiable { } + }