本文翻译自:JPA and Hibernate - Criteria vs. JPQL or HQL

What are the pros and cons of using Criteria or HQL ? 使用Criteria或HQL有什么利弊? The Criteria API is a nice object-oriented way to express queries in Hibernate, but sometimes Criteria Queries are more difficult to understand/build than HQL. Criteria API是在Hibernate中表达查询的一种很好的面向对象的方式,但是有时候Criteria Queries比HQL更难以理解/构建。

When do you use Criteria and when HQL? 您何时使用标准以及何时使用HQL? What do you prefer in which use cases? 在哪种用例中,您更喜欢什么? Or is it just a matter of taste? 还是只是口味问题?


#1楼

参考:https://stackoom.com/question/pN4/JPA和Hibernate-条件与JPQL或HQL


#2楼

For me Criteria is a quite easy to Understand and making Dynamic queries. 对我而言,条件很容易理解和进行动态查询。 But the flaw i say so far is that It loads all many-one etc relations because we have only three types of FetchModes ie Select, Proxy and Default and in all these cases it loads many-one (may be i am wrong if so help me out :)) 但是到目前为止,我说的缺点是它加载所有多对一关系,因为我们只有三种类型的FetchModes,即选择,代理和默认,并且在所有这些情况下,它都加载多对一(如果这样的话,我是错的我出来:))

2nd issue with Criteria is that it loads complete object ie if i want to just load EmpName of an employee it wont come up with this insted it come up with complete Employee object and i can get EmpName from it due to this it really work bad in reporting . Criteria的第二个问题是它加载了完整的对象,即,如果我只想加载一个雇员的EmpName,它不会提出这个insted,它会提出完整的Employee对象,由于这个问题,我真的可以从中获取EmpName 报告where as HQL just load(did't load association/relations) what u want so increase performance many times. 就像HQL只是加载(不加载关联/关系)那样,您想要的东西因此可以多次提高性能。

One feature of Criteria is that it will safe u from SQL Injection because of its dynamic query generation where as in HQL as ur queries are either fixed or parameterised so are not safe from SQL Injection. Criteria的一项功能是,由于它具有动态查询生成功能,因此它可以保护您免受SQL Injection的侵害,因为在HQL中,由于ur的查询是固定的或参数化的,因此对于SQL Injection是不安全的。

Also if you write HQL in ur aspx.cs files, then you are tightly coupled with ur DAL. 另外,如果您在ur aspx.cs文件中编写HQL,那么您与UR DAL紧密相关。

Overall my conclusion is that there are places where u can't live without HQL like reports so use them else Criteria is more easy to manage. 总的来说,我的结论是,有些地方像报告一样无法没有HQL,因此可以使用它们,否则Criteria更加易于管理。


#3楼

Criteria are the only way to specify natural key lookups that take advantage of the special optimization in the second level query cache. 条件是指定自然键查找的唯一方法,该方法利用二级查询缓存中的特殊优化。 HQL does not have any way to specify the necessary hint. HQL没有任何方法可以指定必要的提示。

You can find some more info here: 您可以在这里找到更多信息:

  • http://tech.puredanger.com/2009/07/10/hibernate-query-cache/ http://tech.puredanger.com/2009/07/10/hibernate-query-cache/

#4楼

对于动态的条件查询,我们可以根据输入来构造查询。如果Hql查询是静态查询,则一旦构造,就无法更改查询的结构。


#5楼

HQL可能引起安全问题,例如SQL注入。


#6楼

Criteria Api is one of the good concept of Hibernate. Criteria Api是Hibernate的良好概念之一。 according to my view these are the few point by which we can make difference between HQL and Criteria Api 根据我的观点,这些是我们可以区分HQL和Criteria Api的几点

  1. HQL is to perform both select and non-select operations on the data, but Criteria is only for selecting the data, we cannot perform non-select operations using criteria. HQL将对数据执行选择和非选择操作,但是Criteria仅用于选择数据,我们不能使用条件执行非选择操作。
  2. HQL is suitable for executing Static Queries, where as Criteria is suitable for executing Dynamic Queries HQL适合执行静态查询,而Criteria适合执行动态查询
  3. HQL doesn't support pagination concept, but we can achieve pagination with Criteria. HQL不支持分页概念,但是我们可以使用Criteria实现分页。
  4. Criteria used to take more time to execute than HQL. 与HQL相比,执行标准所花的时间更多。
  5. With Criteria we are safe with SQL Injection because of its dynamic query generation but in HQL as your queries are either fixed or parametrized, there is no safe from SQL Injection 使用Criteria,我们可以安全地使用SQL Injection,因为它可以动态生成查询,但是在HQL中,由于查询是固定的或参数化的,因此SQL Injection是不安全的

JPA和Hibernate-条件与JPQL或HQL相关推荐

  1. java jpa hibernate_java - JPA和Hibernate - Criteria与JPQL或HQL

    java - JPA和Hibernate - Criteria与JPQL或HQL 使用Criteria或HQL有哪些优缺点? Criteria API是一种很好的面向对象的方式来表达Hibernate ...

  2. JPA入门例子(采用JPA的hibernate实现版本)

    http://blog.csdn.net/hmk2011/article/details/6289151(1).JPA介绍: JPA全称为Java Persistence API ,Java持久化AP ...

  3. 简述 JPA 与 Spring Data JPA 与 Hibernate

    1.JPA是什么?以及相关概述 JPA的是 Java Persistence API 的简写,是Sun官方提出的一种ORM规范! Sun提出此规范有2个原因: 1.简化现有Java EE和Java S ...

  4. jpa命名 多条件查询命名_JPA 2 | 动态查询与命名查询

    jpa命名 多条件查询命名 JPA有自己的查询语言,称为JPQL. JPQL与SQL非常相似,主要区别在于JPQL与应用程序中定义的实体一起使用,而SQL与数据库中定义的表和列名称一起使用. 在定义将 ...

  5. gwt-2.8.2下载_GWT 2 Spring 3 JPA 2 Hibernate 3.5教程

    gwt-2.8.2下载 本分步指南将介绍如何使用以下方法开发简单的Web应用程序 Google的网络工具包 (GWT)用于富客户端,而Spring作为后端服务器端框架. 该示例Web应用程序将提供对数 ...

  6. 【转】JPA、Hibernate和Mybatis区别和总结

    很多人都用过java的数据库连接池C3P0,但官方没有说明名称的由来. 据传闻:连接池作者是<星球大战>迷,C3P0就是其中的一个机器人,并且这个名称中包涵connection 和pool ...

  7. JPA和Hibernate的关系

    转载自:https://www.cnblogs.com/amoyzhu/p/5937717.html JPA Java Persistence API,是Java EE 5的标准ORM接口,也是ejb ...

  8. JPA和Hibernate的关系(转)

    JPA Java Persistence API,是Java EE 5的标准ORM接口,也是ejb3规范的一部分. Hibernate,当今很流行的ORM框架,是JPA的一个实现,但是其功能是JPA的 ...

  9. SpringData Jpa、Hibernate、Jpa 三者之间的关系

    JPA规范与ORM框架之间的关系是怎样的呢? JPA规范本质上就是一种ORM规范,注意不是ORM框架--因为JPA并未提供ORM实现,它只是制订了一些规范,提供了一些编程的API接口,但具体实现则由服 ...

最新文章

  1. 初始化Mysql系统报错,begin failesd--conpilation aborted at scripts........
  2. Eclipse配置外部Tomcat
  3. python 东哥 with open_Python一行代码搞定炫酷可视化,你需要了解一下Cufflinks
  4. 使用 HTML5 Canvas 绘制出惊艳的水滴效果
  5. 深入浅出InfoPath——让管理员来部署InfoPath表单
  6. VM安装虚拟机不能全屏解决
  7. java eclipse profile_在 Eclipse 中集成JProfiler 进行 JAVA 项目性能分析
  8. 有助于改变你命运的5个处世潜规则
  9. 高性能相关----爬虫
  10. dd命令打包多个文件_linux下如何tar打包多个并列文件夹,如:将a文件夹 b文件夹 c文件夹,打包成d.tar文件...
  11. 微信支付:不支持虚拟货币交易 如发现违规行为将予以清退处理
  12. 数据分析的法宝---箱形图(Box Plot)
  13. pip安装第三方库 报错:You should consider upgrading
  14. LRU(Least Recent Used) java实现——为什么采用HashMap+双向链表
  15. 阿文的《Java从入门到精通(第二版)》学习日记DAY1
  16. 如何禁止NavigationController的向右滑动返回
  17. OpenSSL 使用openssl工具搭建私有CA
  18. 关于 MyBatis动态sql,这里有 10 种超好用的写法
  19. 猫眼电影爬虫和数据分析
  20. 双子星IPTV桌面APK源码 网络电视机顶盒直播APP源码 带php后台

热门文章

  1. java 是怎么跨平台运行的
  2. 【day31】函数总结
  3. 初笔,JAVA.HelloWorld代码详解
  4. html5利用websocket完成的推送功能
  5. spring 4.0下集成webservice
  6. shell自动交互之expect脚本_转
  7. 福利月|这个月你想要什么书
  8. 自制操作系统(五) 保护模式寻址原理、字符鼠标指针显示
  9. .NET C#获取当前网页地址信息
  10. ELK下logstash收集java日志,多行合并成一行