Skip to content

Commit

Permalink
Eliminate unnecessary merge() if possible
Browse files Browse the repository at this point in the history
  • Loading branch information
quaff committed Mar 18, 2024
1 parent a3cf247 commit a1a688b
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
* @author Oliver Gierke
* @author Thomas Darimont
* @author Mark Paluch
* @author Yanming Zhou
*/
public interface JpaEntityInformation<T, ID> extends EntityInformation<T, ID>, JpaEntityMetadata<T> {

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

/**
* Returns the version attribute of the entity.
*/
@Nullable
SingularAttribute<? super T, ?> getVersionAttribute();

/**
* Returns the required identifier type.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,15 @@

/**
* Implementation of {@link org.springframework.data.repository.core.EntityInformation} that uses JPA {@link Metamodel}
* to find the domain class' id field.
* to find the domain class' id and version field.
*
* @author Oliver Gierke
* @author Thomas Darimont
* @author Christoph Strobl
* @author Mark Paluch
* @author Jens Schauder
* @author Greg Turnquist
* @author Yanming Zhou
*/
public class JpaMetamodelEntityInformation<T, ID> extends JpaEntityInformationSupport<T, ID> {

Expand Down Expand Up @@ -191,6 +192,11 @@ public Class<ID> getIdType() {
return idMetadata.getSimpleIdAttribute();
}

@Override
public SingularAttribute<? super T, ?> getVersionAttribute() {
return versionAttribute.orElse(null);
}

@Override
public boolean hasCompositeId() {
return !idMetadata.hasSimpleId();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,8 @@ public void delete(T entity) {
return;
}

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

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
*
* @author Oliver Gierke
* @author Jens Schauder
* @author Yanming Zhou
*/
@ExtendWith(MockitoExtension.class)
@MockitoSettings(strictness = Strictness.LENIENT)
Expand Down Expand Up @@ -88,6 +89,11 @@ static class DummyJpaEntityInformation<T, ID> extends JpaEntityInformationSuppor
return null;
}

@Override
public SingularAttribute<? super T, ?> getVersionAttribute() {
return null;
}

@Override
public ID getId(T entity) {
return null;
Expand Down

0 comments on commit a1a688b

Please sign in to comment.