title: Can't call database function inside CriteriaBuilder construct method
body: <p>I simplified my DTO class to only have 2 properties: UUID and a Long:</p>
  <pre><code>public class MyDto implements Serializable {
  
      private UUID uuid;
      private Long sequential;
  
      // Constructor taking properties as parameters
  
      // Geters and setters
  }
  </code></pre>
  <p>And my code works great when I try to construct in CriteriaBuilder like this:</p>
  <pre><code>// Assuming my constructor in MyDto take only UUID parameter
  
  query.select(builder.construct(MyDto.class,
              root.get(MyDto_.uuid)));
  </code></pre>
  <p>But when I try to add the 'sequential' parameter in the constructor of my Dto, and change my select to retrieve the value from a db function as bellow:</p>
  <pre><code>// Assuming my constructor in MyDto take both UUID and Long parameters
  
  query.select(builder.construct(MyDto.class,
              builder.function(&quot;nextval&quot;, Long.class, builder.literal(&quot;my_seq&quot;)),
              root.get(MyDto_.uuid)));
  </code></pre>
  <p>hibernate throw a exception:</p>
  <pre><code>java.lang.NullPointerException
  at org.hibernate.hql.internal.NameGenerator.generateColumnNames(NameGenerator.java:27)
  at org.hibernate.hql.internal.ast.util.SessionFactoryHelper.generateColumnNames(SessionFactoryHelper.java:418)
  at org.hibernate.hql.internal.ast.tree.SelectClause.initializeColumnNames(SelectClause.java:269)
  at org.hibernate.hql.internal.ast.tree.SelectClause.finishInitialization(SelectClause.java:259)
  at org.hibernate.hql.internal.ast.tree.SelectClause.initializeExplicitSelectClause(SelectClause.java:254)
  at org.hibernate.hql.internal.ast.HqlSqlWalker.useSelectClause(HqlSqlWalker.java:991)
  at org.hibernate.hql.internal.ast.HqlSqlWalker.processQuery(HqlSqlWalker.java:759)
  at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:675)
  at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:311)
  at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:259)
  at org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:262)
  at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:190)
  at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:142)
  at org.hibernate.engine.query.spi.HQLQueryPlan.&lt;init&gt;(HQLQueryPlan.java:115)
  at org.hibernate.engine.query.spi.HQLQueryPlan.&lt;init&gt;(HQLQueryPlan.java:77)
  at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:153)
  at org.hibernate.internal.AbstractSharedSessionContract.getQueryPlan(AbstractSharedSessionContract.java:545)
  at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:654)
  at org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:3282)
  at org.hibernate.query.criteria.internal.CriteriaQueryImpl$1.buildCompiledQuery(CriteriaQueryImpl.java:318)
  at org.hibernate.query.criteria.internal.compile.CriteriaCompiler.compile(CriteriaCompiler.java:127)
  at org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:3575)
  at org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:204)
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  at java.lang.reflect.Method.invoke(Method.java:498)
  at org.jboss.weld.bean.proxy.AbstractBeanInstance.invoke(AbstractBeanInstance.java:38)
  at org.jboss.weld.bean.proxy.ProxyMethodHandler.invoke(ProxyMethodHandler.java:100)
  at org.jboss.weld.proxies.AutoCloseable$EntityManager$HibernateEntityManager$QueryProducer$Serializable$Session$SharedSessionContract$1645305853$Proxy$_$$_WeldClientProxy.createQuery(Unknown Source)
  </code></pre>
  <p><a href="https://github.com/vladmihalcea/high-performance-java-persistence/blob/master/core/src/test/java/com/vladmihalcea/book/hpjp/hibernate/query/function/GroupConcatFunctionDialectRegisterTest.java#L53" rel="nofollow noreferrer">This is an example</a> of what I'm trying to do, <a href="https://gist.github.com/thjanssen/6bc1df67975ae93bff90b5cd73136a48#file-source-java" rel="nofollow noreferrer">and here</a> I see the function being used in a where, but my need is to use in the constructor of CriteriaBuilder.</p>
  <p>I have this working in the old way <code>SELECT NEW MyDto(...)</code> but Im strugling with CriteriaBuilder.</p>