开发者

Hibernate... how does one do database/SQL queries?

开发者 https://www.devze.com 2023-01-07 19:45 出处:网络
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.

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开发者_StackOverflow中文版, and process multiple results?

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


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

  • 14. HQL: The Hibernate Query Language
  • 15. Criteria Queries
  • 16. Native SQL


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

0

精彩评论

暂无评论...
验证码 换一张
取 消