使用Criteria或HQL的优点和缺点是什么?Criteria API是一种在Hibernate中表达查询的面向对象的好方法,但有时Criteria queries比HQL更难理解/构建。
什么时候使用标准,什么时候使用HQL?在哪些用例中您更喜欢什么?还是说这只是个人口味的问题?
使用Criteria或HQL的优点和缺点是什么?Criteria API是一种在Hibernate中表达查询的面向对象的好方法,但有时Criteria queries比HQL更难理解/构建。
什么时候使用标准,什么时候使用HQL?在哪些用例中您更喜欢什么?还是说这只是个人口味的问题?
当前回答
Criteria Api是Hibernate的一个很好的概念。根据我的观点,这些是我们可以区分HQL和Criteria Api的少数点
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 is suitable for executing Static Queries, where as Criteria is suitable for executing Dynamic Queries HQL doesn’t support pagination concept, but we can achieve pagination with Criteria. Criteria used to take more time to execute than HQL. 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是一个面向对象的API,而HQL意味着字符串连接。这意味着面向对象的所有好处都适用:
All else being equal, the OO version is somewhat less prone to error. Any old string could get appended into the HQL query, whereas only valid Criteria objects can make it into a Criteria tree. Effectively, the Criteria classes are more constrained. With auto-complete, the OO is more discoverable (and thus easier to use, for me at least). You don't necessarily need to remember which parts of the query go where; the IDE can help you You also don't need to remember the particulars of the syntax (like which symbols go where). All you need to know is how to call methods and create objects.
由于HQL非常像SQL(大多数开发人员已经非常了解SQL),因此这些“不必记住”参数就没有那么重要了。如果HQL有更大的不同,那么这就更重要了。
条件是指定利用第二级查询缓存中的特殊优化的自然键查找的唯一方法。HQL没有任何方法来指定必要的提示。
你可以在这里找到更多信息:
http://tech.puredanger.com/2009/07/10/hibernate-query-cache/
We used mainly Criteria in our application in the beginning but after it was replaced with HQL due to the performance issues. Mainly we are using very complex queries with several joins which leads to multiple queries in Criteria but is very optimized in HQL. The case is that we use just several propeties on specific object and not complete objects. With Criteria the problem was also string concatenation. Let say if you need to display name and surname of the user in HQL it is quite easy (name || ' ' || surname) but in Crteria this is not possible. To overcome this we used ResultTransormers, where there were methods where such concatenation was implemented for needed result. Today we mainly use HQL like this:
String hql = "select " +
"c.uuid as uuid," +
"c.name as name," +
"c.objective as objective," +
"c.startDate as startDate," +
"c.endDate as endDate," +
"c.description as description," +
"s.status as status," +
"t.type as type " +
"from " + Campaign.class.getName() + " c " +
"left join c.type t " +
"left join c.status s";
Query query = hibernateTemplate.getSessionFactory().getCurrentSession().getSession(EntityMode.MAP).createQuery(hql);
query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
return query.list();
因此,在本例中,返回的记录是所需属性的映射。
对我来说,Criteria是一个很容易理解的动态查询。但我说到目前为止的缺陷是,它加载所有的多-一等关系,因为我们只有三种类型的FetchModes,即选择,代理和默认,在所有这些情况下,它加载多-一(可能是我错了,如果这样帮助我:))
第二个问题与标准是,它加载完整的对象,即,如果我想只是加载一个员工的EmpName,它不会提出这个,而是提出完整的员工对象,我可以从它得到EmpName,因为这真的工作不好的报告。因为HQL只是加载(没有加载关联/关系)你想要的,所以性能会提高很多倍。
Criteria的一个特点是,它将使你免受SQL注入的伤害,因为它的动态查询生成,而在HQL中,ur的查询要么是固定的,要么是参数化的,因此在SQL注入中不安全。
同样,如果你在你的aspx.cs文件中编写HQL,那么你与你的DAL紧密耦合。
总的来说,我的结论是,有些地方你不能没有HQL,比如报告,所以使用它们,否则Criteria更容易管理。
为了两全其美,HQL的表达性和简洁性以及Criteria的动态特性可以考虑使用Querydsl。
Querydsl支持JPA/Hibernate, JDO, SQL和Collections。
我是Querydsl的维护者,所以这个答案是有偏见的。