使用Criteria或HQL的优点和缺点是什么?Criteria API是一种在Hibernate中表达查询的面向对象的好方法,但有时Criteria queries比HQL更难理解/构建。

什么时候使用标准,什么时候使用HQL?在哪些用例中您更喜欢什么?还是说这只是个人口味的问题?


当前回答

条件是指定利用第二级查询缓存中的特殊优化的自然键查找的唯一方法。HQL没有任何方法来指定必要的提示。

你可以在这里找到更多信息:

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

其他回答

对我来说,Criteria是一个很容易理解的动态查询。但我说到目前为止的缺陷是,它加载所有的多-一等关系,因为我们只有三种类型的FetchModes,即选择,代理和默认,在所有这些情况下,它加载多-一(可能是我错了,如果这样帮助我:))

第二个问题与标准是,它加载完整的对象,即,如果我想只是加载一个员工的EmpName,它不会提出这个,而是提出完整的员工对象,我可以从它得到EmpName,因为这真的工作不好的报告。因为HQL只是加载(没有加载关联/关系)你想要的,所以性能会提高很多倍。

Criteria的一个特点是,它将使你免受SQL注入的伤害,因为它的动态查询生成,而在HQL中,ur的查询要么是固定的,要么是参数化的,因此在SQL注入中不安全。

同样,如果你在你的aspx.cs文件中编写HQL,那么你与你的DAL紧密耦合。

总的来说,我的结论是,有些地方你不能没有HQL,比如报告,所以使用它们,否则Criteria更容易管理。

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();

因此,在本例中,返回的记录是所需属性的映射。

对于动态查询,我也更喜欢使用条件查询。但我更喜欢hql删除查询,例如,如果从父id 'xyz'的子表中删除所有记录,这很容易由hql实现,但对于标准API,首先我们必须触发n个删除查询,其中n是子表记录的数量。

这里的大多数答案都是误导性的,并提到标准查询比HQL慢,但事实并非如此。

如果你深入研究并执行一些测试,你会发现标准查询比常规HQL执行得更好。

同样,通过Criteria Query,你可以得到HQL所没有的面向对象的控制。

更多信息请阅读这里的答案。

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有更大的不同,那么这就更重要了。