Java createCriteria 和 createAlias 的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18062286/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 22:08:42  来源:igfitidea点击:

Difference between createCriteria and createAlias

javahibernate

提问by commit

I was reading hibernate criteria document here:

我在这里阅读休眠标准文档:

http://docs.jboss.org/hibernate/orm/3.6/javadocs/org/hibernate/Criteria.html

http://docs.jboss.org/hibernate/orm/3.6/javadocs/org/hibernate/Criteria.html

I had used it many time and I am normally use createAlias()to join table, here they have provide two methods to make a join and fetch data from the tables, there are:

我已经用过很多次了,我通常使用createAlias()来连接表,这里他们提供了两种方法来进行连接并从表中获取数据,有:

 List cats = session.createCriteria(Cat.class)
     .createCriteria("kittens")
         .add( Restrictions.like("name", "Iz%") )
     .list();

 List cats = session.createCriteria(Cat.class)
     .createAlias("kittens", "kit")
     .add( Restrictions.like("kit.name", "Iz%") )
     .list();

So I am not able to distinguish difference between .createCriteria("kittens")and createAlias("kittens", "kit")or may be I am not getting what this code exactly do, can someone help me to clear my confusion.

所以我无法区分.createCriteria("kittens")createAlias("kittens", "kit")或可能是我没有得到这段代码究竟做了什么,有人可以帮我清除我的困惑。

回答by Ankur Lathi

The only difference is that CreateCriteriahas 2 additional overloads without the alias parameter, this difference is long gone in the latest versions.

But essentially the application is slightly different in its usage is that CreateCriteriauses its relations of mapping from parent to child, whilst with CreateAliasyou defined them with your customized alias names from the root.

唯一的区别是CreateCriteria有 2 个没有别名参数的额外重载,这种区别在最新版本中早已消失。

但本质上,该应用程序的用法略有不同,它CreateCriteria使用了从父级到子级的映射关系,而CreateAlias您使用根目录中的自定义别名定义了它们

Read more from here.

这里阅读更多。

回答by harrybvp

Main Difference is that Criterias' createCriteria()creates and returns Sub Criteria (new Criteria Object).This is useful if you want to create criteria for subquery.
Here is what documentation says about its return type

主要区别在于Criterias' createCriteria()创建和返回子条件(新条件对象)。如果您想为子查询创建条件,这很有用。
这是文档关于其返回类型的说明

Returns:
the created "sub criteria"

返回:
创建的“子条件”

Criteria's CreateAlias()returns existing Criteria Object
Here is what documentation says about its return type

Criteria's CreateAlias()返回现有的 Criteria 对象
这里是关于它的返回类型的文档说明

Returns:
this (for method chaining)

返回:
this(用于方法链接)