Когда я пытаюсь использовать CriteriaBuilder и entityManager для получения набора атрибутов из объекта с именем SeedRecord, я получаю следующую ошибку:
org.hibernate.hql.internal.ast.QuerySyntaxException: невозможно найти соответствующий конструктор в классе [DTOs.EntityDTOs.AffiliateLinksPerSeedSummaryDTO]. Ожидаемые аргументы: long, java.lang.String, java.lang.String, POJOs.AffiliateLink [выберите новый DTOs.EntityDTOs.AffiliateLinksPerSeedSummaryDTO (сгенерированныйAlias0.id, сгенерированныйAlias0.varietyName, сгенерированныйAlias0.produceName1, сгенерированныйAlias0.produceName), сгенерированныйAlias0.produceName. внутреннее соединение generatedAlias0.affiliateLinks как generatedAlias1]
Согласно трассировке стека, мой класс-оболочка AffiliateLinksPerSeedSummaryDTO должен иметь конструктор, включающий поле типа Pojo.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());
}
fetch
может ?? - person Antoniossss   schedule 06.08.2018