我无法一起使用Spring Data JPA投影和规范.我有以下设置:
实体:
@Entity public class Country { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; @Column(name = "NAME", nullable = false) private String name; @Column(name = "CODE", nullable = false) private String code; ---getters & setters--- }
投影界面:
public interface CountryProjection { String getName(); }
国家规格:
public class CountrySpecification { public static SpecificationpredicateName(final String name) { return new Specification () { @Override public Predicate toPredicate(Root eventRoot, CriteriaQuery> criteriaQuery, CriteriaBuilder criteriaBuilder) { return criteriaBuilder.equal(eventRoot.get(Country_.name), name); } }; } }
库:
public interface CountryRepository extends JpaRepository, JpaSpecificationExecutor { List findByName(String name); // works fine List findAllProjectedBy(); // works fine List findAllProjectedBy(Specification specification); //throws Exception as shown below }
前两个方法findByName和findAllProjectedBy工作正常.而第三种方法findAllProjectedBy(规范规范)抛出以下异常 -
引起:java.util.NoSuchElementException:java.util.ArrayList中的null $ Itr.next(ArrayList.java:854)〜[na:1.8.0_102] at java.util.Collections $ UnmodifiableCollection $ 1.next(Collections.java :1042)〜[na:1.8.0_102] org.springframework.data.jpa.repository.query.CriteriaQueryParameterBinder.bind(CriteriaQueryParameterBinder.java:63)〜[spring-data-jpa-1.10.6.RELEASE.jar: na] org.springframework.data.jpa.repository.query.ParameterBinder.bind(ParameterBinder.java:100)〜[spring-data-jpa-1.10.6.RELEASE.jar:na] org.springframework.data. org.springframework.data.jpa.repository.query.ParameterBinder.bindAndPrepare中的jpa.repository.query.ParameterBinder.bindAndPrepare(ParameterBinder.java:160)〜[spring-data-jpa-1.10.6.RELEASE.jar:na] (ParameterBinder.java:151)〜[spring-data-jpa-1.10.6.RELEASE.jar:na] at org.springframework.data.jpa.repository.query.PartTreeJpaQuery $ QueryPreparer.invokeBinding(PartTreeJpaQuery.java:218) 〜[spring-data-jpa-1.10.6.RELEASE.jar:na] org.springframework.data.jpa.r epository.query.PartTreeJpaQuery $ QueryPreparer.createQuery(PartTreeJpaQuery.java:142)〜[spring-data-jpa-1.10.6.RELEASE.jar:na] at org.springframework.data.jpa.repository.query.PartTreeJpaQuery.doCreateQuery (PartTreeJpaQuery.java:78)〜[spring-data-jpa-1.10.6.RELEASE.jar:na] at org.springframework.data.jpa.repository.query.AbstractJpaQuery.createQuery(AbstractJpaQuery.java:190)~ [ spring-data-jpa-1.10.6.RELEASE.jar:na] at org.springframework.data.jpa.repository.query.JpaQueryExecution $ CollectionExecution.doExecute(JpaQueryExecution.java:118)~ [spring-data-jpa-1.10 .6.RELEASE.jar:na] at org.springframework.data.jpa.repository.query.JpaQueryExecution.execute(JpaQueryExecution.java:82)〜[spring-data-jpa-1.10.6.RELEASE.jar:na]在org.springframework.data.jpa.repository.query.AbstractJpaQuery.doExecute(AbstractJpaQuery.java:116)〜[spring-data-jpa-1.10.6.RELEASE.jar:na] org.springframework.data.jpa. repository.query.AbstractJpaQuery.execute(AbstractJpaQuery.java:106)〜[spring-data-jpa-1.10.6.R ELEASE.jar:na] at org.springframework.data.repository.core.support.RepositoryFactorySupport $ QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:482)〜[spring-data-commons-1.12.6.RELEASE.jar:na] at org.springframework.data.repository.core.support.RepositoryFactorySupport $ QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:460)〜[spring-data-commons-1.12.6.RELEASE.jar:na] org.springframework.aop.framework .ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)〜[spring-aop-4.3.5.RELEASE.jar:4.3.5.RELEASE] org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:61) 〜[spring-data-commons-1.12.6.RELEASE.jar:na] org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)〜[spring-aop-4.3.5.RELEASE.jar :4.3.5.RELEASE] at org.springframework.transaction.interceptor.TransactionInterceptor $ 1.proceedWithInvocation(Transaction Interceptor.java:99)〜[spring-tx-4.3.5.RELEASE.jar:4.3.5.RELEASE] org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:282)〜[spring-tx -4.3.5.RELEASE.jar:4.3.5.RELEASE] at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)〜[spring-tx-4.3.5.RELEASE.jar:4.3. 5.RELEASE]在org.springframework.dao的org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)〜[spring-aop-4.3.5.RELEASE.jar:4.3.5.RELEASE]. org.springframework.aop.framework.ReflectiveMethodInvocation.proceed中的support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136)〜[spring-tx-4.3.5.RELEASE.jar:4.3.5.RELEASE](ReflectiveMethodInvocation.java:179 )〜[spring-aop-4.3.5.RELEASE.jar:4.3.5.RELEASE] org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor $ CrudMethodMetadataPopulating 在Org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)〜[spring]的MethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:133)〜[spring-data-jpa-1.10.6.RELEASE.jar:na] -aop-4.3.5.RELEASE.jar:4.3.5.RELEASE]在org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)〜[spring-aop-4.3.5.RELEASE.jar: 4.3.5.RELEASE]在org.springframework的org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)〜[spring-aop-4.3.5.RELEASE.jar:4.3.5.RELEASE].在com.sun.proxy上的aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213)〜[spring-aop-4.3.5.RELEASE.jar:4.3.5.RELEASE.$ Proxy82.findAllProjectedBy(Unknown Source)〜 [na:na] at com.mmp.data.jpa.DataJpaApplication.run(DataJpaApplication.java:42)[classes /:na] at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:800)[spring- boot-1.4.3.RELEASE.jar:1.4.3.RELEASE]...省略了11个常用帧
怎么能实现这一目标?有任何想法吗?
目前尚不支持混合投影和规格的功能.有一个错误跟踪这个.
我发现这个https://github.com/pramoth/specification-with-projection,它似乎工作正是你正在寻找的.我把它包含在我自己的项目中,到目前为止没有任何问题.非常感谢Pramoth.
基本上你扩展JpaSpecificationExecutorWithProjection而不是JpaSpecificationExecutor.
public interface DocumentRepository extends JpaRepository< Country,Long>,JpaSpecificationExecutorWithProjection
并且你得到了带有投影和规范的findall()方法
Page findAll(Specification spec, Class projectionClass, Pageable pageable);