Skip to content

Commit a1a688b

Browse files
committed
Eliminate unnecessary merge() if possible
Fix spring-projectsGH-3401
1 parent a3cf247 commit a1a688b

File tree

4 files changed

+22
-2
lines changed

4 files changed

+22
-2
lines changed

spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/support/JpaEntityInformation.java

+7
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
* @author Oliver Gierke
3131
* @author Thomas Darimont
3232
* @author Mark Paluch
33+
* @author Yanming Zhou
3334
*/
3435
public interface JpaEntityInformation<T, ID> extends EntityInformation<T, ID>, JpaEntityMetadata<T> {
3536

@@ -39,6 +40,12 @@ public interface JpaEntityInformation<T, ID> extends EntityInformation<T, ID>, J
3940
@Nullable
4041
SingularAttribute<? super T, ?> getIdAttribute();
4142

43+
/**
44+
* Returns the version attribute of the entity.
45+
*/
46+
@Nullable
47+
SingularAttribute<? super T, ?> getVersionAttribute();
48+
4249
/**
4350
* Returns the required identifier type.
4451
*

spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/support/JpaMetamodelEntityInformation.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,15 @@
4747

4848
/**
4949
* Implementation of {@link org.springframework.data.repository.core.EntityInformation} that uses JPA {@link Metamodel}
50-
* to find the domain class' id field.
50+
* to find the domain class' id and version field.
5151
*
5252
* @author Oliver Gierke
5353
* @author Thomas Darimont
5454
* @author Christoph Strobl
5555
* @author Mark Paluch
5656
* @author Jens Schauder
5757
* @author Greg Turnquist
58+
* @author Yanming Zhou
5859
*/
5960
public class JpaMetamodelEntityInformation<T, ID> extends JpaEntityInformationSupport<T, ID> {
6061

@@ -191,6 +192,11 @@ public Class<ID> getIdType() {
191192
return idMetadata.getSimpleIdAttribute();
192193
}
193194

195+
@Override
196+
public SingularAttribute<? super T, ?> getVersionAttribute() {
197+
return versionAttribute.orElse(null);
198+
}
199+
194200
@Override
195201
public boolean hasCompositeId() {
196202
return !idMetadata.hasSimpleId();

spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/support/SimpleJpaRepository.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,8 @@ public void delete(T entity) {
195195
return;
196196
}
197197

198-
entityManager.remove(entityManager.contains(entity) ? entity : entityManager.merge(entity));
198+
boolean versioned = entityInformation.getVersionAttribute() != null; // GH-3401
199+
entityManager.remove((entityManager.contains(entity) || !versioned) ? entity : entityManager.merge(entity));
199200
}
200201

201202
@Override

spring-data-jpa/src/test/java/org/springframework/data/jpa/repository/support/JpaEntityInformationSupportUnitTests.java

+6
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
*
4343
* @author Oliver Gierke
4444
* @author Jens Schauder
45+
* @author Yanming Zhou
4546
*/
4647
@ExtendWith(MockitoExtension.class)
4748
@MockitoSettings(strictness = Strictness.LENIENT)
@@ -88,6 +89,11 @@ static class DummyJpaEntityInformation<T, ID> extends JpaEntityInformationSuppor
8889
return null;
8990
}
9091

92+
@Override
93+
public SingularAttribute<? super T, ?> getVersionAttribute() {
94+
return null;
95+
}
96+
9197
@Override
9298
public ID getId(T entity) {
9399
return null;

0 commit comments

Comments
 (0)