Thursday, June 08, 2006

Creating an EJBQL query using a constructor expression in the SELECT clause

I'd read you could construct an entity summary dto direct from an EJBQL query. After a little fiddling I got it to work as follows:


public List listEntitySummaries() {
String query
= "select new com.myapp.model.dto.MyEntitySummaryDTO(e.id, e.title) from MyEntity e";

return (List) em.createQuery(query).getResultList();
}


The DTO in this case was a very simple java bean. I didn't need to declare it in persistence.xml. I did have to use the fully qualified classname however otherwise Toplink (in my case) couldn't find the constructor.

1 comment:

Anonymous said...
This comment has been removed by a blog administrator.