Skip to content

Commit

Permalink
Polishing.
Browse files Browse the repository at this point in the history
Reuse comment hint retrieval. Reorder methods. Inline methods and use fluent API where possible.

See #2991
Original pull request: #2995
  • Loading branch information
mp911de committed Jun 2, 2023
1 parent 03cb819 commit 48cb338
Showing 1 changed file with 52 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.BiConsumer;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
Expand Down Expand Up @@ -230,9 +231,11 @@ public void deleteAllByIdInBatch(Iterable<ID> ids) {
entityInformation.getIdAttribute().getName());

Query query = em.createQuery(queryString);
/**

/*
* Some JPA providers require {@code ids} to be a {@link Collection} so we must convert if it's not already.
*/

if (Collection.class.isInstance(ids)) {
query.setParameter("ids", ids);
} else {
Expand Down Expand Up @@ -304,33 +307,11 @@ public Optional<T> findById(ID id) {
}

LockModeType type = metadata.getLockModeType();

Map<String, Object> hints = new HashMap<>();

getQueryHints().withFetchGraphs(em).forEach(hints::put);

if (metadata != null && metadata.getComment() != null && provider.getCommentHintKey() != null) {
hints.put(provider.getCommentHintKey(), provider.getCommentHintValue(metadata.getComment()));
}
Map<String, Object> hints = getHints();

return Optional.ofNullable(type == null ? em.find(domainType, id, hints) : em.find(domainType, id, type, hints));
}

/**
* Returns {@link QueryHints} with the query hints based on the current {@link CrudMethodMetadata} and potential
* {@link EntityGraph} information.
*/
protected QueryHints getQueryHints() {
return metadata == null ? NoHints.INSTANCE : DefaultQueryHints.of(entityInformation, metadata);
}

/**
* Returns {@link QueryHints} with the query hints on the current {@link CrudMethodMetadata} for count queries.
*/
protected QueryHints getQueryHintsForCount() {
return metadata == null ? NoHints.INSTANCE : DefaultQueryHints.of(entityInformation, metadata).forCounts();
}

@Deprecated
@Override
public T getOne(ID id) {
Expand Down Expand Up @@ -437,7 +418,7 @@ public List<T> findAll(Sort sort) {
@Override
public Page<T> findAll(Pageable pageable) {

if (isUnpaged(pageable)) {
if (pageable.isUnpaged()) {
return new PageImpl<>(findAll());
}

Expand All @@ -463,7 +444,7 @@ public List<T> findAll(Specification<T> spec) {
public Page<T> findAll(Specification<T> spec, Pageable pageable) {

TypedQuery<T> query = getQuery(spec, pageable);
return isUnpaged(pageable) ? new PageImpl<>(query.getResultList())
return pageable.isUnpaged() ? new PageImpl<>(query.getResultList())
: readPage(query, getDomainClass(), pageable, spec);
}

Expand All @@ -475,9 +456,12 @@ public List<T> findAll(Specification<T> spec, Sort sort) {
@Override
public boolean exists(Specification<T> spec) {

CriteriaQuery<Integer> cq = this.em.getCriteriaBuilder().createQuery(Integer.class);
cq.select(this.em.getCriteriaBuilder().literal(1));
CriteriaQuery<Integer> cq = this.em.getCriteriaBuilder() //
.createQuery(Integer.class) //
.select(this.em.getCriteriaBuilder().literal(1));

applySpecificationToCriteria(spec, getDomainClass(), cq);

TypedQuery<Integer> query = applyRepositoryMethodMetadata(this.em.createQuery(cq));
return query.setMaxResults(1).getResultList().size() == 1;
}
Expand Down Expand Up @@ -565,9 +549,12 @@ public <S extends T> long count(Example<S> example) {
public <S extends T> boolean exists(Example<S> example) {

Specification<S> spec = new ExampleSpecification<>(example, this.escapeCharacter);
CriteriaQuery<Integer> cq = this.em.getCriteriaBuilder().createQuery(Integer.class);
cq.select(this.em.getCriteriaBuilder().literal(1));
CriteriaQuery<Integer> cq = this.em.getCriteriaBuilder() //
.createQuery(Integer.class) //
.select(this.em.getCriteriaBuilder().literal(1));

applySpecificationToCriteria(spec, example.getProbeType(), cq);

TypedQuery<Integer> query = applyRepositoryMethodMetadata(this.em.createQuery(cq));
return query.setMaxResults(1).getResultList().size() == 1;
}
Expand All @@ -590,7 +577,7 @@ public <S extends T> Page<S> findAll(Example<S> example, Pageable pageable) {
Class<S> probeType = example.getProbeType();
TypedQuery<S> query = getQuery(new ExampleSpecification<>(example, escapeCharacter), probeType, pageable);

return isUnpaged(pageable) ? new PageImpl<>(query.getResultList()) : readPage(query, probeType, pageable, spec);
return pageable.isUnpaged() ? new PageImpl<>(query.getResultList()) : readPage(query, probeType, pageable, spec);
}

@Override
Expand Down Expand Up @@ -804,6 +791,21 @@ protected <S extends T> TypedQuery<Long> getCountQuery(@Nullable Specification<S
return applyRepositoryMethodMetadataForCount(em.createQuery(query));
}

/**
* Returns {@link QueryHints} with the query hints based on the current {@link CrudMethodMetadata} and potential
* {@link EntityGraph} information.
*/
protected QueryHints getQueryHints() {
return metadata == null ? NoHints.INSTANCE : DefaultQueryHints.of(entityInformation, metadata);
}

/**
* Returns {@link QueryHints} with the query hints on the current {@link CrudMethodMetadata} for count queries.
*/
protected QueryHints getQueryHintsForCount() {
return metadata == null ? NoHints.INSTANCE : DefaultQueryHints.of(entityInformation, metadata).forCounts();
}

/**
* Applies the given {@link Specification} to the given {@link CriteriaQuery}.
*
Expand Down Expand Up @@ -854,10 +856,7 @@ private void applyQueryHints(Query query) {
}

getQueryHints().withFetchGraphs(em).forEach(query::setHint);

if (metadata.getComment() != null && provider.getCommentHintKey() != null) {
query.setHint(provider.getCommentHintKey(), provider.getCommentHintValue(metadata.getComment()));
}
applyComment(metadata, query::setHint);
}

private <S> TypedQuery<S> applyRepositoryMethodMetadataForCount(TypedQuery<S> query) {
Expand All @@ -878,9 +877,26 @@ private void applyQueryHintsForCount(Query query) {
}

getQueryHintsForCount().forEach(query::setHint);
applyComment(metadata, query::setHint);
}

private Map<String, Object> getHints() {

Map<String, Object> hints = new HashMap<>();

getQueryHints().withFetchGraphs(em).forEach(hints::put);

if (metadata != null) {
applyComment(metadata, hints::put);
}

return hints;
}

private void applyComment(CrudMethodMetadata metadata, BiConsumer<String, Object> consumer) {

if (metadata.getComment() != null && provider.getCommentHintKey() != null) {
query.setHint(provider.getCommentHintKey(), provider.getCommentHintValue(metadata.getComment()));
consumer.accept(provider.getCommentHintKey(), provider.getCommentHintValue(this.metadata.getComment()));
}
}

Expand All @@ -903,10 +919,6 @@ private static long executeCountQuery(TypedQuery<Long> query) {
return total;
}

private static boolean isUnpaged(Pageable pageable) {
return pageable.isUnpaged();
}

/**
* Specification that gives access to the {@link Parameter} instance used to bind the ids for
* {@link SimpleJpaRepository#findAllById(Iterable)}. Workaround for OpenJPA not binding collections to in-clauses
Expand Down

0 comments on commit 48cb338

Please sign in to comment.