2017-01-17 27 views
3

私はバネデータの投影と仕様を一緒に使用することはできません。ばねデータjpaで投影と仕様を使用する方法?

ENTITY-

@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正常に動作します。 〜 java.util.ArrayListの$ Itr.next(ArrayList.java:854)でヌル:java.util.NoSuchElementException:

に起因 - 第三の方法findAllProjectedBy(仕様書)一方 は、次の例外をスロー[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] 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)で 〜[春-データ-JPA-1.10.6.RELEASE.jar:ナ] 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]に(spring-data-jpa-1.10.6.RELEASE.jar:na) 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] (ReflectiveMethodInvocation.java:179) 〜[spring-aop-4.3.5。 RELEASE.jar:4.3.5.RELEASE] org.springframework.transaction.interceptor.TransactionInterceptor $ 1.proceedWithInvocation(TransactionInterceptor.java:99) 〜[spring-tx-4.3.5.RELEASE.jar:4.3.5。 [リリース] at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:282) にある 〜[spring-tx-4.3.5.RELEASE.jar:4.3.5.RELEASE] 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] でorg.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor $ CrudMethodPopulatingMethodInterceptor.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 (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] com.sun.proxy。$ Proxy82.findAllProjectedBy(不明なソース)〜[na:na] com.mmp.data.jpa.DataJpaApplication.run(DataJpaApplication .java:42) [classes /:na] 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に大きな感謝。

基本的には、JpaSpecificationExecutorの代わりにJpaSpecificationExecutorWithProjectionを拡張します。

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

、あなたが予測と仕様

<R> Page<R> findAll(Specification<T> spec, Class<R> projectionClass, Pageable pageable); 
とのfindAll()メソッドを取得します
関連する問題