Hi,
any idea what is wrong with my DTO Projection in the code below ?
I would like to select data with DTO Projection using the “Hibernate Criteria API”. Is it at all possible, or should i use JPQL,… ?
Hibernate expects a constructor with all the selected attributes although AliasToBeanResultTransformer is specified as transforming strategy (actually this way it should not be necessary to implement
any constructor with parameters). Or i have got it wrong ?
I don’t want to implement the expected constructor because my original query is a bit complex (with some joins,…) and it selects a lot of informations…
I would be highly grateful for your information
The error i get is :
“QuerySyntaxException: Unable to locate appropriate constructor on class MessageDetailsDTO… Expected arguments are: int, org.joda.time.DateTime [select new de.stroeer.core.message.dto.MessageListMessageDetailsDTO(generatedAlias0.id, generatedAlias0.creationTime)”
Here is my Code:
@Entity
public class Message {
public Message() {
}
@Id
private long id;
private DateTime creationTime;
// getter and setter methods
}
public class MessageDetailsDTO {
public MessageDetailsDTO() {
}
private long id;
private DateTime creationTime;
// getter and setter methods
}
CriteriaBuilder builder = getEntityManager().getCriteriaBuilder();
CriteriaQuery<MessageDetailsDTO> criteriaQuery = builder.createQuery(MessageDetailsDTO.class);
Root<Message> root = criteriaQuery.from(Message.class);
criteriaQuery =
criteriaQuery.multiselect(root.get(Message_.id), root.get(Message_.creationTime)) //
.where(root.get(MessageBean_.id).in(ids));
List<MessageDetailsDTO> result =
getEntityManager().createQuery(criteriaQuery)//
.unwrap(org.hibernate.Query.class)
.setResultTransformer(new AliasToBeanResultTransformer(MessageDetailsDTO.class))
.list();