'How to map TypedQuery result to custom object list

I need to make some select and map it to custom 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);
    }
}

but

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

not helped.

I got an Exception

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

Is this possible to map TypedQuery to some custom DTOs?



Solution 1:[1]

DTO projections using a Constructor Expression and JPQL:

select new com.package.SomeDTO(p.id, p.title) from EntityCalss p 

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Peter Šály