如何将TypedQuery结果映射到自定义对象列表

时间:2019-03-20 16:06:25

标签: hibernate jpa query-builder hibernate-criteria resulttransformer

我需要进行一些选择并将其映射到自定义DTO

public class SomeClass {
    @PersistenceContext
    private EntityManager em;

    public void doSomething() {
        CriteriaBuilder builder = em.getCriteriaBuilder();
        CriteriaQuery<SomeClass2> criteria = builder.createQuery(SomeClass2.class);
        Root<SomeClass2> root = criteria.from(SomeClass2.class);
        Join<SomeClass3, SomeClass2> join = root.join("field");

        ... other joins and predicates ...

        em.createQuery(criteria);
    }
}

但是

em.createQuery(criteria)
    .unwrap(org.hibernate.query.Query.class)
    .setResultTransformer(Transformers.aliasToBean(CustomDTO.class));

没有帮助。

我有例外

org.hibernate.hql.internal.ast.QuerySyntaxException: Unable to locate appropriate constructor on class [net.package.api.SomeClass2]. 
Expected arguments are: java.lang.String, java.lang.String, Java.lang.String, long

是否可以将TypedQuery映射到某些自定义DTO?

1 个答案:

答案 0 :(得分:0)

使用构造函数表达式和JPQL的DTO投影:

let data = [2, 4, 5, 8, 1, 9, 3];
let faulty = [2, 1, 5, 6];

let clean = data.filter(d => faulty.indexOf(d) < 0 );

console.log(clean);