All the above queries tells us specially scalar values, basically returning the "raw" values from the resultset. In the following code shows how to get entity objects from a native sql query via addEntity().
sess.createSQLQuery("SELECT * FROM STUDENT").addEntity(Student.class);
sess.createSQLQuery("SELECT ID, NAME, BIRTHDATE FROM STUDENT").addEntity(Student.class);
By the help of this query we found:
1. the SQL query string
2. the entity returned by the query
Assuming that Student is mapped as a class with the columns ID, NAME and BIRTHDATE the above queries will both return a List where each element is a Student entity. If the entity is mapped with a many-to-one to another entity it is required to also return this when performing the native query, otherwise a database specific "column not found" error will occur. The additional columns will automatically be returned when using the * notation, but we prefer to be explicit as in the following example for a many-to-one to a Person:
sess.createSQLQuery("SELECT ID, NAME, BIRTHDATE, PERSON_ID FROM PERSON").addEntity(Person.class);
Above code allow student.getPerson() to function properly.