我试图使用和query按查询执行group。
但是,我有以下例外:-
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类下面提供:
自定义接口:
public interface JapanWHT21940000DataRepositoryCustom {
List<WHT21940000Royalties> findLocalCcyAmtsByRemarks();
}自定义Impl类:
@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接口:
public interface JapanWHT21940000DataRepository
extends JpaRepository<WHT21940000Data, Long>,
QueryDslPredicateExecutor<WHT21940000Data>,
JapanWHT21940000DataRepositoryCustom {
}和DAO类:
@Repository
@Transactional("japanWhtTransactionManager")
public class JapanWHTDaoImpl implements JapanWHTDao {
@Autowired
JapanWHT21940000DataRepository wht21940000DataRepo;
// more code to follow...编辑:还是在Spring + query中是否有一种比我所尝试的更简单、更好的按查询分组的方法?
发布于 2017-09-27 16:59:58
我认为真正的问题是执行来自JpaRepositories的自定义querydsl查询,不过我必须建议使用自定义扩展的基本存储库类,如下所示。
首先是扩展的基本存储库类。
@NoRepositoryBean
public interface ExtendedQueryDslJpaRepository<T, ID extends Serializable> extends JpaRepository<T, ID>, QueryDslPredicateExecutor<T> {
<T1> Page<T1> findAll(JPQLQuery jpqlQuery, Pageable pageable);
}以及相关的实施。
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类应该扩展的默认类,如下所示:
@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。
public interface CustomRepository extends ExtendedQueryDslJpaRepository<CustomEntity, Long>, CustomRepositorySupport {
}接下来,我们为自定义方法定义定义接口CustomRepositorySupport。
public interface CustomRepositorySupport {
JPQLQuery<CustomListDto> createCustomIndexQuery();
}最后是自定义存储库实现。
@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类调用方法,如下所示。
public interface CustomFinder {
Page<CustomListDto> findIndex(Pageable pageable);
}-
@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 by或projections。
这看起来可能有点过于复杂,但是如果您选择实现它,并看到它是如何工作的,您将坚持它。它正在成功地应用于实际的应用程序,提供对投影查询的访问(在不需要的地方加载完整的实体)、分页查询、存在子查询等等。
最后,使用一种中央库类是一种只编写一次的可重用代码。
希望这能有所帮助。
https://stackoverflow.com/questions/46445854
复制相似问题