JPQL: How to "SELECT new Foo(null, null... someValue, ..)?
You can try (but just a try)
SELECT new Foo(cast(null as string), cast(null as string), f.col3, f.col4)
FROM Foo f
or looking about cast() operator for JPQL if it is supported (I'm not sure)
else
Session session = entityManager.unwrap(Session.class);
List<Foo> list = session.createQuery("select f.col3, f.col4 from Foo f").list()
This is Hibernate specific and you don't need specific constructor for every projections; just create an empty constructor Foo()
(at least protected, I don't remember if private is allowed) and let Hibernate inject value into col3, col4
from your query (and cast()
operator is supported if your underlying database support the function).
I know this question is old but you can also do (if cast didn't work for you):
String query = "select new Pojo( " +
" trim(:null), " +
" trim(:null), " +
" f.col3, " +
" f.col4 " +
" ) " +
" from Foo as f ";
return em.createQuery(query, Pojo.class)
.setParameter("null", null)
.getResultList();
Edited: JPA ignores literal columns when mapping them to the constructor arguments, so wrapped with trim to let JPA aware of the literal value.