Native SQL
Native SQL statements are also supported in Hibernate, except you will need to use a different API call to compose a query object. There are mainly two reasons you would want to use a native SQL. The first is that you wish to use a specific feature offered by your database solution that may not be supported by Hibernate. The second is when you wish to execute an ad hoc query and store the result in an object that is not a Hibernate entity.
If you don't associate an entity with the query result, Hibernate returns the result set in a list of object array. This is known as a Scalar Query. However, you can associate an entity with the native query and Hibernate will return a list of this entity; this is known as Entity query. Let's see how these work.
Scalar query
When you execute a native SQL, the result of this query is returned in a list of object arrays. Consider the following query:
List<Object[]> objectList = session .createSQLQuery("SELECT * FROM PERSON") .list();
Each element...