我如何查询主键字段

时间:2014-12-01 12:09:58

标签: spring-data spring-data-jpa

我有一个实体从JPA扩展AbstractPersistable类。

import org.springframework.data.jpa.domain.AbstractPersistable;

public class Test extends AbstractPersistable<Long> {
    ...
}

此外,我的规格看起来像这样:

public static Specification<Test> gt(final Long id) {
    return new Specification<Test>() {

        @Override
        public Predicate toPredicate(Root<Test> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
            return cb.gt((Expression) root.get("id"), id);
        }
    };
}

我的存储库:

@Repository
@Transactional
public interface TestRepository extends JpaRepository<Test, JpaSpecificationExecutor<Test> {}

如果我查询存储库

testRepository.findAll(TestSpecifications.gt(1);

我收到以下错误:

Caused by: java.lang.IllegalArgumentException: Unaware how to convert value [1 : java.lang.Integer] to requested type [java.io.Serializable]
at org.hibernate.jpa.criteria.ValueHandlerFactory.unknownConversion(ValueHandlerFactory.java:258)
at org.hibernate.jpa.criteria.ValueHandlerFactory.convert(ValueHandlerFactory.java:286)
at org.hibernate.jpa.criteria.predicate.ComparisonPredicate.<init>(ComparisonPredicate.java:94)
at org.hibernate.jpa.criteria.CriteriaBuilderImpl.gt(CriteriaBuilderImpl.java:482)
at com.domcollect.javalib.data.filter.DefaultSpecifications$8.toPredicate(DefaultSpecifications.java:127)
at org.springframework.data.jpa.repository.support.SimpleJpaRepository.applySpecificationToCriteria(SimpleJpaRepository.java:537)
at org.springframework.data.jpa.repository.support.SimpleJpaRepository.getQuery(SimpleJpaRepository.java:488)
at org.springframework.data.jpa.repository.support.SimpleJpaRepository.getQuery(SimpleJpaRepository.java:473)
at org.springframework.data.jpa.repository.support.SimpleJpaRepository.findAll(SimpleJpaRepository.java:355)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.executeMethodOn(RepositoryFactorySupport.java:442)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:427)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:381)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:98)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:266)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:95)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136)
... 84 more

问题是,ValueHandlerFactory没有检查该值是否已经是Serializable的实例。

他们的解决方案是什么?

0 个答案:

没有答案