使用Criteria或HQL的优点和缺点是什么?Criteria API是一种在Hibernate中表达查询的面向对象的好方法,但有时Criteria queries比HQL更难理解/构建。
什么时候使用标准,什么时候使用HQL?在哪些用例中您更喜欢什么?还是说这只是个人口味的问题?
使用Criteria或HQL的优点和缺点是什么?Criteria API是一种在Hibernate中表达查询的面向对象的好方法,但有时Criteria queries比HQL更难理解/构建。
什么时候使用标准,什么时候使用HQL?在哪些用例中您更喜欢什么?还是说这只是个人口味的问题?
当前回答
HQL和criteriaQuery在性能上是有区别的,每次你使用criteriaQuery发起查询时,它会为表名创建一个新的别名,这个别名不会反映在任何DB的最后一个查询缓存中。这会导致编译生成的SQL的开销,需要更多的时间来执行。
关于抓取策略[http://www.hibernate.org/315.html]
Criteria respects the laziness settings in your mappings and guarantees that what you want loaded is loaded. This means one Criteria query might result in several SQL immediate SELECT statements to fetch the subgraph with all non-lazy mapped associations and collections. If you want to change the "how" and even the "what", use setFetchMode() to enable or disable outer join fetching for a particular collection or association. Criteria queries also completely respect the fetching strategy (join vs select vs subselect). HQL respects the laziness settings in your mappings and guarantees that what you want loaded is loaded. This means one HQL query might result in several SQL immediate SELECT statements to fetch the subgraph with all non-lazy mapped associations and collections. If you want to change the "how" and even the "what", use LEFT JOIN FETCH to enable outer-join fetching for a particular collection or nullable many-to-one or one-to-one association, or JOIN FETCH to enable inner join fetching for a non-nullable many-to-one or one-to-one association. HQL queries do not respect any fetch="join" defined in the mapping document.
其他回答
对我来说,Criteria是一个很容易理解的动态查询。但我说到目前为止的缺陷是,它加载所有的多-一等关系,因为我们只有三种类型的FetchModes,即选择,代理和默认,在所有这些情况下,它加载多-一(可能是我错了,如果这样帮助我:))
第二个问题与标准是,它加载完整的对象,即,如果我想只是加载一个员工的EmpName,它不会提出这个,而是提出完整的员工对象,我可以从它得到EmpName,因为这真的工作不好的报告。因为HQL只是加载(没有加载关联/关系)你想要的,所以性能会提高很多倍。
Criteria的一个特点是,它将使你免受SQL注入的伤害,因为它的动态查询生成,而在HQL中,ur的查询要么是固定的,要么是参数化的,因此在SQL注入中不安全。
同样,如果你在你的aspx.cs文件中编写HQL,那么你与你的DAL紧密耦合。
总的来说,我的结论是,有些地方你不能没有HQL,比如报告,所以使用它们,否则Criteria更容易管理。
另一点是,我认为Criteria更适合在它的基础上构建,而不是在最终代码中直接使用。
它更适合使用它来构建库,而不是使用jpql或hql。
例如,我使用Criteria API构建了spring-data-jpa-mongodb-expressions(与spring数据QBE的方式相同)。
我认为spring数据查询代使用jpaql而不是标准,我不明白为什么。
HQL可能会导致SQL注入等安全问题。
条件是指定利用第二级查询缓存中的特殊优化的自然键查找的唯一方法。HQL没有任何方法来指定必要的提示。
你可以在这里找到更多信息:
http://tech.puredanger.com/2009/07/10/hibernate-query-cache/
HQL和criteriaQuery在性能上是有区别的,每次你使用criteriaQuery发起查询时,它会为表名创建一个新的别名,这个别名不会反映在任何DB的最后一个查询缓存中。这会导致编译生成的SQL的开销,需要更多的时间来执行。
关于抓取策略[http://www.hibernate.org/315.html]
Criteria respects the laziness settings in your mappings and guarantees that what you want loaded is loaded. This means one Criteria query might result in several SQL immediate SELECT statements to fetch the subgraph with all non-lazy mapped associations and collections. If you want to change the "how" and even the "what", use setFetchMode() to enable or disable outer join fetching for a particular collection or association. Criteria queries also completely respect the fetching strategy (join vs select vs subselect). HQL respects the laziness settings in your mappings and guarantees that what you want loaded is loaded. This means one HQL query might result in several SQL immediate SELECT statements to fetch the subgraph with all non-lazy mapped associations and collections. If you want to change the "how" and even the "what", use LEFT JOIN FETCH to enable outer-join fetching for a particular collection or nullable many-to-one or one-to-one association, or JOIN FETCH to enable inner join fetching for a non-nullable many-to-one or one-to-one association. HQL queries do not respect any fetch="join" defined in the mapping document.