开发者

Hibernate HQL or Criteria

I am new to hibernate. I saw some query example are written in HQL and some are written by us开发者_如何学编程ing criteria and projection. I googled around and found little information about when to use HQL and when to use criteria. which one is more preferred way and why?


If your query requires a dynamic number of fields in the where clause, and you dont know a priori, use criteria. If your query always has the same where clause, but you possibly might substitute values, use hql.

You can also use criteria in the latter case as well.

The main point is don't dynamically construct your hql where clause with a bunch of string concatenations.


I prefer hql over criteria and suggest using hql whenever possible.

With hql you can create named queries, which are parsed at startup time, giving you early feedback if there are errors. The pre-parsing also makes them a bit more efficient at runtime, I believe.

I also consider hql much more readable than criteria code.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜