I have been using Hibernate 3.3 with success by mapping 1 (one) database table to 1 (one) entity class automatically by using HQL query. In my new project however, I am using a native SQL query that is fetching data from 4 different tables. (I will use stored procedures and functions in the future, that's why I need the native SQL query). I want to assign the returned values to 1 entity class. My understanding is that I need to use result column aliases (or placeholders).
On page 284 of the book "Hibernate in Action" there is an example of placeholders - the names enclosed in braces:
String sql = "select u.USER_ID as {uzer.id}," + " u.FIRSTNAME as {uzer.firstname}," + " u.LASTNAME as {uzer.lastname} from USERS u";
List results = session.createSQLQuery(sql, "uzer", User.class).list();
If my understanding is correct here, Hibernate returns the column USER_ID of the table USERS and assigns it to the field id of the User class via the alias/placeholder "uzer", the same way it assigns the column USERNAME to the field firstname of the same entity class, LASTNAME to lastname, etc. Well, this was Hibernate version 2, I think, and this method is deprecated in version 3. And I couldn't find a similar example in the 2nd version of this book (called "Java Persistence with Hibernate").
Can someone please tell me (preferably with example code) how to do the same thing in version 3.3, or do I need to go back to version 2 or use another ORM tool?
Any help is much appreciated.
|