Skip to content

Commit

Permalink
DATAJPA-568 - Fix is-new detection for primitive version properties.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
christophstrobl authored and odrotbohm committed Jul 10, 2014
1 parent 820d3ef commit 84a6d92
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
*
* @author Oliver Gierke
* @author Thomas Darimont
* @author Christoph Strobl
*/
public class JpaMetamodelEntityInformation<T, ID extends Serializable> extends JpaEntityInformationSupport<T, ID> {

Expand Down Expand Up @@ -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);
}

Expand All @@ -203,7 +204,7 @@ public boolean isNew(T entity) {
return true;
}

return versionAttribute.getJavaType().isPrimitive() && ((Number) versionValue).longValue() == 0;
return ((Number) versionValue).longValue() == 0;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
*
* @author Oliver Gierke
* @author Thomas Darimont
* @author Christoph Strobl
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({ "classpath:infrastructure.xml" })
Expand Down Expand Up @@ -182,6 +183,39 @@ public void considersEntityWithPrimitiveVersionPropertySetToDefaultNew() {
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";
}
Expand All @@ -207,4 +241,5 @@ public static abstract class Identifiable {
public static class Sample extends Identifiable {

}

}

0 comments on commit 84a6d92

Please sign in to comment.