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

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


当前回答

另一点是,我认为Criteria更适合在它的基础上构建,而不是在最终代码中直接使用。

它更适合使用它来构建库,而不是使用jpql或hql。

例如,我使用Criteria API构建了spring-data-jpa-mongodb-expressions(与spring数据QBE的方式相同)。

我认为spring数据查询代使用jpaql而不是标准,我不明白为什么。

其他回答

对于动态查询,我更喜欢使用条件查询。例如,根据某些参数,动态地添加一些排序或保留一些部分(例如限制)要容易得多。

另一方面,我使用HQL进行静态和复杂的查询,因为它更容易理解/阅读HQL。此外,我认为HQL更强大一些,例如对于不同的连接类型。

另一点是,我认为Criteria更适合在它的基础上构建,而不是在最终代码中直接使用。

它更适合使用它来构建库,而不是使用jpql或hql。

例如,我使用Criteria API构建了spring-data-jpa-mongodb-expressions(与spring数据QBE的方式相同)。

我认为spring数据查询代使用jpaql而不是标准,我不明白为什么。

对我来说,Criteria最大的优势是示例API,在这里你可以传递一个对象,hibernate将基于这些对象属性构建一个查询。

除此之外,标准API也有它的怪癖(我相信hibernate团队正在重做API),比如:

a criteria.createAlias("obj")强制使用内部连接而不是可能的外部连接 您不能两次创建相同的别名 有些SQL子句没有简单的对应条件(比如子选择) 等。

当我想要类似于sql的查询时,我倾向于使用HQL(从用户中删除status='blocked'),当我不想使用字符串追加时,我倾向于使用标准。

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

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

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