Hibernate Native Sql

You can also express queries in the native SQL dialect of your database. This is useful if you want to utilize database-specific features such as query hints or the CONNECT keyword in Oracle. It also provides a clean migration path from a direct SQL/JDBC based application to Hibernate.

Hibernate3 allows you to specify handwritten SQL, including stored procedures, for all create, update, delete, and load operations.

Scalar queries

The most basic SQL query is to get a list of scalars (values).

session.createSQLQuery("SELECT name FROM EMPLOYEE");

These will return a List of Object arrays (Object[]) with scalar values for each column in the EMPLOYEE table.

Entity queries

The above queries were all about returning scalar values, basically returning the “raw” values from the resultset. The following shows how to get entity objects from a native sql query via addEntity().

session.createSQLQuery("SELECT name FROM EMPLOYEE");
You can download the source code of this example here.

One Response to Hibernate Native Sql