Как реализовать метод hashCode () для Spring JPA (составной) Спецификация - PullRequest
0 голосов
/ 30 марта 2020

Я создал REST-приложение Spring Boot, и мне нужен стабильный хэш-код для org.springframework.data.jpa.domain.Specifications.

У меня есть несколько репозиториев:

@Transactional
@PreAuthorize("isAuthenticated()")
public interface ContactRepository extends JpaRepository<Contact, Long>, ContactCustomRepository<Contact, Long>, JpaSpecificationExecutor<Contact> {

    @Cacheable(cacheNames = "contacts")
    @Override
    Page findAll(Specification specification, Pageable pageable);

Поскольку я использую кеш, Мне нужен ключ solid, и я обнаружил, что проблема в том, что я не контролирую спецификации, когда они составлены с помощью методов .and () .or (), и я не могу переопределить метод hashCode (). Как вы видите в реализации Spring, методы .and () .or () возвращают составной объект.

     /**
     * ANDs the given {@link Specification} to the current one.
     *
     * @param other can be {@literal null}.
     * @return The conjunction of the specifications
     * @since 2.0
     */
    @Nullable
    default Specification<T> and(@Nullable Specification<T> other) {
        return composed(this, other, (builder, left, rhs) -> builder.and(left, rhs));
    }

Я не нашел способа настроить hascode для составных спецификаций. Есть ли у вас какие-либо предложения?

...