This is not necessary, but will certainly help to use
From https://docs.jboss.org/hibernate/orm/3.3/reference/en/html/querysql.html
The simplest SQL query is to get a list of scalars (values).
sess.createSQLQuery("SELECT * FROM CATS").list(); sess.createSQLQuery("SELECT ID, NAME, BIRTHDATE FROM CATS").list();
They will return an array of List Objects (Object []) with scalar values ββfor each column in the CATS table. Hibernate will use ResultSetMetadata to infer the actual order and return types of scalar values.
To avoid the overhead of using ResultSetMetadata or just to be more explicit in what is returned, addScalar () can be used:
sess.createSQLQuery("SELECT * FROM CATS") .addScalar("ID", Hibernate.LONG) .addScalar("NAME", Hibernate.STRING) .addScalar("BIRTHDATE", Hibernate.DATE)
The specified request:
SQL query string columns and types to return
This will return arrays of objects, but now it will not use ResultSetMetadata, but instead will explicitly get the column ID, NAME and BIRTHDATE, respectively, Long, String and Short from the base result set.
It also means that only these three columns will be returned, although the query uses * and can return more than three listed columns.
You can leave type information for all or some scalars.
sess.createSQLQuery("SELECT * FROM CATS") .addScalar("ID", Hibernate.LONG) .addScalar("NAME") .addScalar("BIRTHDATE")
This is essentially the same query as before, but now ResultSetMetaData is used to determine the types NAME and BIRTHDATE, where when the identifier type is explicitly specified.
How java.sql.Types is returned from ResultSetMetaData mapped to Hibernate types is controlled by the dialect. If a particular type is not displayed or does not result in the expected type, it can be configured using calls to registerHibernateType in the dialect.