使用criteriabuilder和entitymanager

时间:2018-08-06 11:09:42

标签: java hibernate hibernate-criteria hibernate-entitymanager

当我尝试使用CriteriaBuilder和EntityManager从名为SeedRecord的实体中获取一组属性时,出现以下错误:

  

org.hibernate.hql.internal.ast.QuerySyntaxException:无法找到   类上的适当构造函数   [DTOs.EntityDTOs.AffiliateLinksPerSeedSummaryDTO]。预期参数   是:long,java.lang.String,java.lang.String,POJOs.AffiliateLink   [选择新   DTOs.EntityDTOs.AffiliateLinksPerSeedSummaryDTO(generatedAlias0.id,   generateAlias0.varietyName,generateAlias0.produceName,   POJOs.SeedRecord中的generateAlias1)作为generateAlias0内部联接   generateAlias0.affiliateLinks如generateAlias1]

根据stacktrace,我的AffiliateLinksPerSeedSummaryDTO包装器类应具有一个构造函数,该构造函数包括一个类型为Pojo.AffiliateLink的字段。但是实际字段是对一组Affiliatelink的引用,而不是对单个Affiliatelink行的引用,因此这没有任何意义。谁能告诉我这可能是什么原因?这是包装器类的代码:

public class AffiliateLinksPerSeedSummaryDTO {
    private long id;
    private String varietyName;
    private String produceName;
    @JsonIgnore
    private Set<AffiliateLink> affiliateLinks;
    private long specificCount;
    private long generalCount;

    public AffiliateLinksPerSeedSummaryDTO(long id, String varietyName, String produceName, Set<AffiliateLink> affiliateLinks) {
        this.id = id;
        this.varietyName = varietyName;
        this.produceName = produceName;
        this.affiliateLinks = affiliateLinks;
    }

所有字段名称都与SeedRecord字段名称完全相同。

这里有我用来获取所需字段/对象的代码段。

    EntityManager em = seedRecordDao.getEntityManager();
    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<AffiliateLinksPerSeedSummaryDTO> criteria = builder.createQuery(AffiliateLinksPerSeedSummaryDTO.class);
    Root<SeedRecord> seedRecordRoot = criteria.from(SeedRecord.class);
    criteria.select(
            builder.construct(
                    AffiliateLinksPerSeedSummaryDTO.class,
                    seedRecordRoot.get("id"),
                    seedRecordRoot.get("varietyName"),
                    seedRecordRoot.get("produceName"),
                    seedRecordRoot.join("affiliateLinks")));

    List<AffiliateLinksPerSeedSummaryDTO> affiliateLinksPerSeed = em.createQuery(criteria).getResultList();

编辑:我最近使事情工作的尝试是通过检索元组而不是使用包装对象。现在的问题是,尽管数据库中有值,结果列表仍为零。这就是我现在所做的:

    public Set<AffiliateLinksPerSeedSummaryDTO> getAffiliateLinksPerSeed(){
        EntityManager em = seedRecordDao.getEntityManager();
        CriteriaBuilder builder = em.getCriteriaBuilder();
        Metamodel m = em.getMetamodel();
        EntityType<SeedRecord> SeedRecord_ = m.entity(SeedRecord.class);
        CriteriaQuery<Tuple> criteria = builder.createTupleQuery();
        Root<SeedRecord> seedRecordRoot = criteria.from(SeedRecord.class);
        Path<Long> idPath = seedRecordRoot.get( "id" );
        Path<String> varietyNamePath = seedRecordRoot.get("varietyName");
        Path<String> produceNamePath = seedRecordRoot.get("produceName");
        Join<SeedRecord,AffiliateLink> affiliateLinks = seedRecordRoot.join("affiliateLinks");
        criteria.multiselect(idPath,varietyNamePath,produceNamePath);

//        criteria.select(
//                builder.construct(
//                        AffiliateLinksPerSeedSummaryDTO.class,
//                        seedRecordRoot.get("id"),
//                        seedRecordRoot.get("varietyName"),
//                        seedRecordRoot.get("produceName"),
//                        seedRecordRoot.get(SeedRecord_.getSet("affiliateLinks",AffiliateLink.class))));

        List<Tuple> affiliateLinksPerSeed = em.createQuery(criteria).getResultList();
        log.info("the count is: " + affiliateLinksPerSeed.size());
                //seedRecordDao.getAffiliateLinksPerSeed();
        return affiliateLinksPerSeed.stream().map(tuple ->{
            Map<Boolean, Long> collect = ((List<AffiliateLink>)tuple.get(affiliateLinks)).stream()
                    .collect(Collectors.partitioningBy(AffiliateLink::isGeneral, Collectors.counting()));
            return new AffiliateLinksPerSeedSummaryDTO(((Long)tuple.get(idPath)), ((String)tuple.get(varietyNamePath)), ((String)tuple.get(produceNamePath)),collect.get(true), collect.get(false));
        }).collect(Collectors.toSet());
    }

1 个答案:

答案 0 :(得分:0)

尝试

seedRecordRoot.joinSet("affiliateLinks")