2017-01-17 56 views
3

我無法一起使用彈簧數據jpa預測和規格。我有以下設置 -如何使用彈簧數據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 Specification<Country> predicateName(final String name) { 
     return new Specification<Country>() { 
      @Override 
      public Predicate toPredicate(Root<Country> eventRoot, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) { 
       return criteriaBuilder.equal(eventRoot.get(Country_.name), name); 
      } 
     }; 
    } 
} 

庫 -

public interface CountryRepository extends JpaRepository<Country, Long>, JpaSpecificationExecutor<Country> { 
    List<CountryProjection> findByName(String name); // works fine 
    List<CountryProjection> findAllProjectedBy(); // works fine 
    List<CountryProjection> findAllProjectedBy(Specification<Country> specification); //throws Exception as shown below 
} 

前兩種方法findByName和findAllProjectedBy工作正常。 而第三方法findAllProjectedBy(規格說明書)拋出以下異常 -

所致:java.util.NoSuchElementException:在 的java.util.ArrayList $ 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] at org.springframework.data.jpa.repository.query.CriteriaQueryParameterBinder .bind(CriteriaQueryParameterBinder.java:63) 〜[spring-data-jpa-1.10.6.RELEASE.jar:na] at org.springframework.data.jpa.repository.query.ParameterBinder.bind(ParameterBinder.java: 100) 〜[spring-data-jpa-1.10.6.RELEASE.jar:na] at org.springframework.data.jpa.repository.query.ParameterBinder.bindAndPrepare(ParameterBinder.java:160) 〜[spring-data- jpa-1.10.6.RELEASE.jar:na] at org.springframework.data.jpa.repository.query.ParameterBinder.bindAndPrepare(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.repository.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] at 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.RELEASE.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] at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) 〜[spring-aop-4.3.5.RELEASE .jar:4.3.5.RELEASE] at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:61) 〜[spring-data-commons-1.12.6.RELEASE.jar:na] at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) 〜[spring-aop-4.3.5。 RELEASE.jar:4.3.5.RELEASE]在 org.springframework.transaction.interceptor.TransactionInterceptor $ 1.proceedWithInvocation(TransactionInterceptor.java:99) 〜[彈簧-TX-4.3.5.RELEASE.jar:4.3.5。 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] at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation。 java:179) 〜[spring-aop-4.3.5.RELEASE.jar:4.3.5.RELEASE] at org.springframework.dao。 support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136) 〜[spring-tx-4.3.5.RELEASE.jar:4.3.5.RELEASE] at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java :179) 〜[spring-aop-4.3.5.RELEASE.jar:4.3.5.RELEASE] at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor $ CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:133) 〜[spring-data-jpa-1.10.6.RELEASE.jar:na] at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) 〜[spring-aop-4.3.5。 RELEASE.jar:4.3.5.RELEASE] at org.springframework.aop.interceptor.ExposeInvocationIn terrier.invoke(ExposeInvocationInterceptor.java:92) 〜[spring-aop-4.3.5.RELEASE.jar:4.3.5.RELEASE] at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179 ) 〜[spring-aop-4.3.5.RELEASE.jar:4.3.5.RELEASE] at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213) 〜[spring-aop-4.3 .5.RELEASE.jar:4.3.5.RELEASE] at com.sun.proxy。$ 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.RELEAS E] ... 11個普通幀 省略

這怎麼能實現?有任何想法嗎?

回答

5

混合投影和規格的功能尚不支持。有一個bug跟蹤這個。

0

如果使用規範,則不能在CountryRepository中使用。

CountryRepository cRepository; 

cRepository.findAll(Specification<Country> specification); 
3

我發現這個https://github.com/pramoth/specification-with-projection,它似乎工作,這正是你正在尋找。我將它包含在我自己的項目中,至今沒有問題。非常感謝Pramoth。

基本上你擴展了JpaSpecificationExecutorWithProjection而不是JpaSpecificationExecutor。

public interface DocumentRepository extends JpaRepository< Country,Long>,JpaSpecificationExecutorWithProjection<Country,Long> 

,你會得到的findAll()方法,預測和規範

<R> Page<R> findAll(Specification<T> spec, Class<R> projectionClass, Pageable pageable); 
相關問題