Well, I think you might be underestimating how little there is in difference between fetching those two queries. Assuming the data is all in the same table, the difference in performance would be negligible.
Regardless, you can always just execute SQL, but yes, to your original point, you simply get back an array with corresonding object types that map to the table.
Quote:
Now one thing to note about native SQL queries is that what gets returned in each element of the List is simply an Object array, containing the datatype to which the queried columns map, as defined by the JPA annotations of the class. Furthermore, with a SELECT * query, we would need to know the order of the columns in the database so we can cast the incoming data properly.
The following is an example of a native SQL query that goes against a User database table with id (Integer), email, name and password fields:
http://www.hiberbook.com/HiberBookWeb/learn.jsp?tutorial=08masteringhqlandnamedqueries
Code:
public static void main(String args[]) {
String sql = "SELECT * FROM USER";
Session session = HibernateUtil.beginTransaction();
SQLQuery query = session.createSQLQuery(sql);
List users = query.list();
for (int i = 0; i < users.size(); i++) {
Object[] o = (Object[]) users.get(i);
System.out.print(((Integer) o[0])); //id
System.out.print(((String) o[1])); //email
System.out.print(((String) o[2])); //name
System.out.println(((String) o[3]));//pass
}
}
http://www.hiberbook.com/HiberBookWeb/learn.jsp?tutorial=08masteringhqlandnamedqueries
Think about the long term maintainability of your program though. If you query on columns, and those columns ever change, your SQL all over you program will change. If you just use the mapped objects, all you have to change is an annotation in one place or another.
-Cameron McKenzie