views:

45

answers:

2

I (think I) understand that Hibernate offers me access to a relational database essentially as if I had a class(es) that matched whatever view(s) I define of some tables.

How do I get the results of a specific query to be accessible via a class? In particular, can I issue a complex SQL query, and process multiple results?

Do I lose any of the power of SQL by using Hibernate?

+1  A: 

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

 sess.createSQLQuery("SELECT ID, NAME, BIRTHDATE FROM CATS").list();

More at http://docs.jboss.org/hibernate/core/3.3/reference/en/html/querysql.html

The MYYN
+3  A: 

I (think I) understand that Hibernate offers me access to a relational database essentially as if I had a class(es) that matched whatever view(s) I define of some tables.

Hibernate provides a framework allowing to map an object model to your database and an API to manipulate data through this object model.

How do I get the results of a specific query to be accessible via a class? In particular, can I issue a complex SQL query, and process multiple results?

I'm not sure I understood the question but let's see. The Hibernate way would be to use HQL (Hibernate Query Language) queries and/or Criteria queries to perform queries on the object model.

But you can also use Native SQL (sacrificing portability) to return entities, multiple entities or even non-managed entities (see also Hibernate 3.2: Transformers for HQL and SQL).

Do I lose any of the power of SQL by using Hibernate?

HQL and the Criteria API are quite powerful - and portable - and will generate the proper SQL for your backend. If required, you can still use native SQL queries as already mentioned. But in most cases, HQL and Criteria work well and should be preferred.

See also

Pascal Thivent
Thank you......
Ira Baxter