Skip to content

Commit 6079cd5

Browse files
GH-2154 - Return new instance after incrementing version property on immutables.
This fixes #2154.
1 parent 5bfabb3 commit 6079cd5

File tree

3 files changed

+79
-13
lines changed

3 files changed

+79
-13
lines changed

src/main/java/org/springframework/data/neo4j/core/mapping/callback/OptimisticLockingSupport.java

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,23 +39,24 @@ Object getAndIncrementVersionPropertyIfNecessary(Object entity) {
3939
Neo4jPersistentEntity<?> neo4jPersistentEntity = (Neo4jPersistentEntity<?>) mappingContext
4040
.getRequiredNodeDescription(entity.getClass());
4141

42-
if (neo4jPersistentEntity.hasVersionProperty()) {
43-
PersistentPropertyAccessor<Object> propertyAccessor = neo4jPersistentEntity.getPropertyAccessor(entity);
44-
Neo4jPersistentProperty versionProperty = neo4jPersistentEntity.getRequiredVersionProperty();
42+
if (!neo4jPersistentEntity.hasVersionProperty()) {
43+
return entity;
44+
}
4545

46-
if (!Long.class.isAssignableFrom(versionProperty.getType())) {
47-
return entity;
48-
}
46+
PersistentPropertyAccessor<Object> propertyAccessor = neo4jPersistentEntity.getPropertyAccessor(entity);
47+
Neo4jPersistentProperty versionProperty = neo4jPersistentEntity.getRequiredVersionProperty();
4948

50-
Long versionPropertyValue = (Long) propertyAccessor.getProperty(versionProperty);
49+
if (!Long.class.isAssignableFrom(versionProperty.getType())) {
50+
return entity;
51+
}
5152

52-
long newVersionValue = 0;
53-
if (versionPropertyValue != null) {
54-
newVersionValue = versionPropertyValue + 1;
55-
}
53+
Long versionPropertyValue = (Long) propertyAccessor.getProperty(versionProperty);
5654

57-
propertyAccessor.setProperty(versionProperty, newVersionValue);
55+
long newVersionValue = 0;
56+
if (versionPropertyValue != null) {
57+
newVersionValue = versionPropertyValue + 1;
5858
}
59-
return entity;
59+
propertyAccessor.setProperty(versionProperty, newVersionValue);
60+
return propertyAccessor.getBean();
6061
}
6162
}

src/test/java/org/springframework/data/neo4j/integration/imperative/OptimisticLockingIT.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
import org.springframework.context.annotation.Configuration;
3434
import org.springframework.dao.OptimisticLockingFailureException;
3535
import org.springframework.data.neo4j.config.AbstractNeo4jConfig;
36+
import org.springframework.data.neo4j.core.Neo4jTemplate;
37+
import org.springframework.data.neo4j.integration.shared.common.ImmutableVersionedThing;
3638
import org.springframework.data.neo4j.integration.shared.common.VersionedThing;
3739
import org.springframework.data.neo4j.integration.shared.common.VersionedThingWithAssignedId;
3840
import org.springframework.data.neo4j.repository.Neo4jRepository;
@@ -270,6 +272,18 @@ void shouldFailOnDeleteByEntityWithWrongVersion(@Autowired VersionedThingWithAss
270272
assertThatExceptionOfType(OptimisticLockingFailureException.class).isThrownBy(() -> repository.delete(thing));
271273
}
272274

275+
@Test // GH-2154
276+
void immutablesShouldWork(@Autowired Neo4jTemplate neo4jTemplate) {
277+
278+
ImmutableVersionedThing immutableVersionedThing = new ImmutableVersionedThing(23L, "Hello");
279+
280+
immutableVersionedThing = neo4jTemplate.save(immutableVersionedThing);
281+
assertThat(immutableVersionedThing.getMyVersion()).isNotNull();
282+
283+
ImmutableVersionedThing copy = immutableVersionedThing.withMyVersion(4711L).withName("World");
284+
assertThatExceptionOfType(OptimisticLockingFailureException.class).isThrownBy(() -> neo4jTemplate.save(copy));
285+
}
286+
273287
interface VersionedThingRepository extends Neo4jRepository<VersionedThing, Long> {}
274288

275289
interface VersionedThingWithAssignedIdRepository extends Neo4jRepository<VersionedThingWithAssignedId, Long> {}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright 2011-2021 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+
* https://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+
*/
16+
package org.springframework.data.neo4j.integration.shared.common;
17+
18+
import lombok.Getter;
19+
import lombok.With;
20+
21+
import org.springframework.data.annotation.Version;
22+
import org.springframework.data.neo4j.core.schema.Id;
23+
import org.springframework.data.neo4j.core.schema.Node;
24+
25+
/**
26+
* @author Michael J. Simons
27+
*/
28+
@Node
29+
@Getter
30+
public class ImmutableVersionedThing {
31+
32+
@Id
33+
private final Long id;
34+
35+
@Version
36+
@With
37+
private final Long myVersion;
38+
39+
@With
40+
private final String name;
41+
42+
public ImmutableVersionedThing(Long id, String name) {
43+
this(id, null, name);
44+
}
45+
46+
private ImmutableVersionedThing(Long id, Long myVersion, String name) {
47+
this.id = id;
48+
this.myVersion = myVersion;
49+
this.name = name;
50+
}
51+
}

0 commit comments

Comments
 (0)