首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >按查询方式分组DSL + Spring抛出NoSuchElementException

按查询方式分组DSL + Spring抛出NoSuchElementException
EN

Stack Overflow用户
提问于 2017-09-27 10:44:09
回答 1查看 3.2K关注 0票数 1

我试图使用和query按查询执行group。

但是,我有以下例外:-

代码语言:javascript
复制
org.springframework.beans.factory.UnsatisfiedDependencyException: 
Error creating bean with name 'japanWHTDaoImpl': 
   Unsatisfied dependency expressed through field 'wht21940000DataRepo'; 
       nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'japanWHT21940000DataRepository': 
    Invocation of init method failed; nested exception is java.util.NoSuchElementException

我试图编写自定义存储库实现,并在接口和impl类下面提供:

自定义接口:

代码语言:javascript
复制
public interface JapanWHT21940000DataRepositoryCustom {
        List<WHT21940000Royalties> findLocalCcyAmtsByRemarks();
}

自定义Impl类:

代码语言:javascript
复制
@Repository
@Transactional
public class JapanWHT21940000DataRepositoryCustomImpl extends QueryDslRepositorySupport implements JapanWHT21940000DataRepositoryCustom {

    public JapanWHT21940000DataRepositoryCustomImpl(Class<?> domainClass) {
    super(domainClass);
    // TODO Auto-generated constructor stub
    }

    @PersistenceContext
    private EntityManager entityManager;

    @Override
    public List<WHT21940000Royalties> findLocalCcyAmtsByRemarks() {
    QWHT21940000Data wht21940000Data = QWHT21940000Data.wHT21940000Data;
    JPAQuery<WHT21940000Royalties> query = new JPAQuery<WHT21940000Royalties>(entityManager);
    query.from(wht21940000Data).groupBy(wht21940000Data.remarks).select(wht21940000Data.remarks, wht21940000Data.localCcyAmt.sum());

    return null;
    }

}

Spring数据JPA接口:

代码语言:javascript
复制
public interface JapanWHT21940000DataRepository
    extends JpaRepository<WHT21940000Data, Long>, 
        QueryDslPredicateExecutor<WHT21940000Data>, 
        JapanWHT21940000DataRepositoryCustom {

}

和DAO类:

代码语言:javascript
复制
@Repository
@Transactional("japanWhtTransactionManager")
public class JapanWHTDaoImpl implements JapanWHTDao {
    @Autowired
    JapanWHT21940000DataRepository wht21940000DataRepo;
    // more code to follow...

编辑:还是在Spring + query中是否有一种比我所尝试的更简单、更好的按查询分组的方法?

EN

回答 1

Stack Overflow用户

发布于 2017-09-27 16:59:58

我认为真正的问题是执行来自JpaRepositories的自定义querydsl查询,不过我必须建议使用自定义扩展的基本存储库类,如下所示。

首先是扩展的基本存储库类。

代码语言:javascript
复制
@NoRepositoryBean
public interface ExtendedQueryDslJpaRepository<T, ID extends Serializable> extends JpaRepository<T, ID>, QueryDslPredicateExecutor<T> {

    <T1> Page<T1> findAll(JPQLQuery jpqlQuery, Pageable pageable);
}

以及相关的实施。

代码语言:javascript
复制
public class ExtendedQueryDslJpaRepositoryImpl<T, ID extends Serializable>
    extends QueryDslJpaRepository<T, ID> implements ExtendedQueryDslJpaRepository<T, ID> {

    private static final EntityPathResolver DEFAULT_ENTITY_PATH_RESOLVER = SimpleEntityPathResolver.INSTANCE;

    private final EntityPath<T> path;
    private final PathBuilder<T> builder;
    private final Querydsl querydsl;

    private EntityManager entityManager;

    public ExtendedQueryDslJpaRepositoryImpl(JpaEntityInformation<T, ID> entityInformation, EntityManager entityManager) {
        this(entityInformation, entityManager, DEFAULT_ENTITY_PATH_RESOLVER);
    }

    public ExtendedQueryDslJpaRepositoryImpl(JpaEntityInformation<T, ID> entityInformation, EntityManager entityManager, EntityPathResolver
        resolver) {

        super(entityInformation, entityManager);
        this.path = resolver.createPath(entityInformation.getJavaType());
        this.builder = new PathBuilder(this.path.getType(), this.path.getMetadata());
        this.querydsl = new Querydsl(entityManager, this.builder);
        this.entityManager = entityManager;
    }

    @Override
    public <T1> Page<T1> findAll(JPQLQuery jpqlQuery, Pageable pageable) {

        final JPQLQuery<?> countQuery = jpqlQuery;

        JPQLQuery<T1> query = querydsl.applyPagination(pageable, jpqlQuery);

        return PageableExecutionUtils.getPage(query.fetch(), pageable, countQuery::fetchCount);
    }
}

上面的类可以放在配置包中。

然后,我们将ExtendedQueryDslJpaRepositoryImpl定义为JpaRepository类应该扩展的默认类,如下所示:

代码语言:javascript
复制
@Configuration
@EnableJpaRepositories(basePackageClasses = Application.class, repositoryBaseClass = ExtendedQueryDslJpaRepositoryImpl.class)
public class JpaConfig {

    @PersistenceContext
    private EntityManager entityManager;

    @Bean
    public JPAQueryFactory jpaQueryFactory() {
        return new JPAQueryFactory(entityManager);
    }
}

下一步是为应用程序的Entity定义一个存储库,例如。CustomEntity

代码语言:javascript
复制
public interface CustomRepository extends ExtendedQueryDslJpaRepository<CustomEntity, Long>, CustomRepositorySupport {
}

接下来,我们为自定义方法定义定义接口CustomRepositorySupport

代码语言:javascript
复制
public interface CustomRepositorySupport {

    JPQLQuery<CustomListDto> createCustomIndexQuery();
}

最后是自定义存储库实现。

代码语言:javascript
复制
@Repository
public class CustomRepositoryImpl implements CustomRepositorySupport {

    private JPAQueryFactory queryFactory;

    @Autowired
    public CustomRepositoryImpl(JPAQueryFactory queryFactory) {
        this.queryFactory = queryFactory;
    }

    @Override
    public JPQLQuery<CustomListDto> createCustomIndexQuery() {

        QCustomEntity qCustomEntity = QCustomEntity.customEntity;

        BooleanBuilder predicate = new BooleanBuilder();

        // Create predicate as desired
        // predicate.and(...);

        // Create projection of fields
        /* FactoryExpression<CustomListDto> factoryExpression = Projections.bean(CustomListDto.class,
            qCustomEntity.fieldA,
            qCustomEntity.fieldB,
            qCustomEntity.fieldC,
            qCustomEntity.fieldD,
            qCustomEntity.fieldE,
            qCustomEntity.fieldF); */

        return queryFactory.from(qCustomEntity).select(factoryExpression).where(predicate);
    }
}

最后一步实际上是从Service类调用方法,如下所示。

代码语言:javascript
复制
public interface CustomFinder {

    Page<CustomListDto> findIndex(Pageable pageable);
}

-

代码语言:javascript
复制
@Service
public class CustomFinderImpl implements CustomFinder {

    private CustomRepository customRepository;

    @Autowired
    public CustomFinderImpl(CustomRepository customRepository) {
        this.customRepository = customRepository;
    }

    @Override
    public Page<CustomListDto> findIndex(Pageable pageable) {

        JPQLQuery<CustomListDto> query = customRepository.createCustomIndexQuery();

        return customRepository.findAll(query, pageable);
    }
}

与实现public <T1> List<T1> findAll(JPQLQuery query, FactoryExpression<T1> factoryExpression)的方式相同,我们可以简单地实现所需的所需方法,如public <T1> List<T1> findAll(JPQLQuery query, FactoryExpression<T1> factoryExpression)<T1> List<T1> findList(JPQLQuery query, FactoryExpression<T1> factoryExpression)等。

通过这种方式,我们向所有存储库添加了功能,以便利用所有类型的querydsl查询,而不仅仅是group byprojections

这看起来可能有点过于复杂,但是如果您选择实现它,并看到它是如何工作的,您将坚持它。它正在成功地应用于实际的应用程序,提供对投影查询的访问(在不需要的地方加载完整的实体)、分页查询、存在子查询等等。

最后,使用一种中央库类是一种只编写一次的可重用代码。

希望这能有所帮助。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46445854

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档