HQL vs. SQL / Hibernate netbeans HQL editor

2019-06-19 17:48发布

I am teaching my self hibernate, and am quite confused of why i can not just write simple SQL queries.

I find it rather more confusing to use than plain SQL (what I am used to)

PLUS: The NetBeans HQL editor I found pretty annoying, It is harder for me to produce a right query in HQL, then in SQL, and why does the shown SQL differ from actual SQL statements ?

So why use it ? - As it is known that hibernate is very resource extensive, and I believe that hibernate is the reason for our app to running out of memory very often, as during the process of redeploying e.g...

I am very interested in knowing why I should use Hibernate and not plain SQL (mysql) statements !?

And maybe a good link for hibernate queries would be nice ;), I am using this one atm:

But also interested in any good link explaining the setup of the queries, the mapping, underlying construction etc..

Best Regards Alex

1条回答
叼着烟拽天下
2楼-- · 2019-06-19 18:38

HQL is object oriented and it's done with the purpose of working on the Java objects representing your DB tables. A basic advantage is that you can put placeholders like :orderNumber (using the colon simbol) in the HQL query and replace with the value of a variable. For example:

int orderNumber = 685412;
List<Order> l= 
    session.createQuery("from Order where orderNumber = :orderNumber")
    .setParameter("orderNumber",orderNumber).list();

In this way you can modify orderNumber in a simple way, evoiding the classical

String query = "select * from Order where orderNumber = " + orderNumber + "...";

Morover using MySQL syntax would sometimes turn your code not reusable if you migrate your DB to another DBMS. Anyway I'm still not so convinced about the preference on HQL.

Here you can find the full grammar definition.

查看更多
登录 后发表回答