Skip to content

Commit 84a6d92

Browse files
christophstroblodrotbohm
authored andcommitted
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.
1 parent 820d3ef commit 84a6d92

File tree

3 files changed

+62
-4
lines changed

3 files changed

+62
-4
lines changed

Diff for: src/main/java/org/springframework/data/jpa/repository/support/JpaMetamodelEntityInformation.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
*
4444
* @author Oliver Gierke
4545
* @author Thomas Darimont
46+
* @author Christoph Strobl
4647
*/
4748
public class JpaMetamodelEntityInformation<T, ID extends Serializable> extends JpaEntityInformationSupport<T, ID> {
4849

@@ -192,7 +193,7 @@ public Object getCompositeIdAttributeValue(Serializable id, String idAttribute)
192193
@Override
193194
public boolean isNew(T entity) {
194195

195-
if (versionAttribute == null) {
196+
if (versionAttribute == null || versionAttribute.getJavaType().isPrimitive()) {
196197
return super.isNew(entity);
197198
}
198199

@@ -203,7 +204,7 @@ public boolean isNew(T entity) {
203204
return true;
204205
}
205206

206-
return versionAttribute.getJavaType().isPrimitive() && ((Number) versionValue).longValue() == 0;
207+
return ((Number) versionValue).longValue() == 0;
207208
}
208209

209210
/**
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,34 @@
1+
/*
2+
* Copyright 2014 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
116
package org.springframework.data.jpa.domain.sample;
217

318
import javax.persistence.Entity;
19+
import javax.persistence.GeneratedValue;
420
import javax.persistence.Id;
521
import javax.persistence.Version;
622

23+
/**
24+
* @author Oliver Gierke
25+
* @author Christoph Strobl
26+
*/
727
@Entity
828
public class PrimitiveVersionProperty {
929

10-
@Id Long id;
11-
@Version long version;
30+
public @Id @GeneratedValue Long id;
31+
public @Version long version;
32+
33+
public String someValue;
1234
}

Diff for: src/test/java/org/springframework/data/jpa/repository/support/JpaMetamodelEntityInformationIntegrationTests.java

+35
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
*
5353
* @author Oliver Gierke
5454
* @author Thomas Darimont
55+
* @author Christoph Strobl
5556
*/
5657
@RunWith(SpringJUnit4ClassRunner.class)
5758
@ContextConfiguration({ "classpath:infrastructure.xml" })
@@ -182,6 +183,39 @@ public void considersEntityWithPrimitiveVersionPropertySetToDefaultNew() {
182183
assertThat(information.isNew(new PrimitiveVersionProperty()), is(true));
183184
}
184185

186+
/**
187+
* @see DATAJPA-568
188+
*/
189+
@Test
190+
public void considersEntityAsNotNewWhenHavingIdSetAndUsingPrimitiveTypeForVersionProperty() {
191+
192+
EntityInformation<PrimitiveVersionProperty, Serializable> information = new JpaMetamodelEntityInformation<PrimitiveVersionProperty, Serializable>(
193+
PrimitiveVersionProperty.class, em.getMetamodel());
194+
195+
PrimitiveVersionProperty pvp = new PrimitiveVersionProperty();
196+
pvp.id = 100L;
197+
198+
assertThat(information.isNew(pvp), is(false));
199+
}
200+
201+
/**
202+
* @see DATAJPA-568
203+
*/
204+
@Test
205+
public void fallsBackToIdInspectionForAPrimitiveVersionProperty() {
206+
207+
EntityInformation<PrimitiveVersionProperty, Serializable> information = new JpaMetamodelEntityInformation<PrimitiveVersionProperty, Serializable>(
208+
PrimitiveVersionProperty.class, em.getMetamodel());
209+
210+
PrimitiveVersionProperty pvp = new PrimitiveVersionProperty();
211+
pvp.version = 1L;
212+
213+
assertThat(information.isNew(pvp), is(true));
214+
215+
pvp.id = 1L;
216+
assertThat(information.isNew(pvp), is(false));
217+
}
218+
185219
protected String getMetadadataPersitenceUnitName() {
186220
return "metadata";
187221
}
@@ -207,4 +241,5 @@ public static abstract class Identifiable {
207241
public static class Sample extends Identifiable {
208242

209243
}
244+
210245
}

0 commit comments

Comments
 (0)