Red Hat Training

A Red Hat training course is available for JBoss Enterprise Application Platform Common Criteria Certification

8.2. Using native SQL Queries

Now that the result set is described, we are capable of executing the native SQL query. EntityManager provides all the needed APIs. The first method is to use a SQL resultset name to do the binding, the second one uses the entity default mapping (the column returned has to have the same names as the one used in the mapping). A third one (not yet supported by Hibernate entity manager), returns pure scalar results.
String sqlQuery = "select nid, night.night_duration, night.night_date, aid, "
    + "night.area_id, from Night night, Area area where night.area_id = "
    + "and night.night_duration >= ?";
Query q = entityManager.createNativeQuery(sqlQuery, "GetNightAndArea");
q.setParameter( 1, expectedDuration );
This native query returns nights and area based on the GetNightAndArea result set.
String sqlQuery = "select * from tbl_spaceship where owner = ?";
Query q = entityManager.createNativeQuery(sqlQuery, SpaceShip.class);
q.setParameter( 1, "Han" );
The second version is useful when your SQL query returns one entity reusing the same columns as the ones mapped in metadata.