NHibernate - CreateCriteria 与 CreateAlias
假设以下场景:
class Project{
public Job Job;
}
class Job{
public Name;
}
假设我想使用 Criteria API 搜索 Job 名称为“sumthing”的所有项目。
我可以使用 CreateAlias 为 Job 创建别名并使用它来访问 Name,或者我可以为属性 Job 创建一个新条件并按 Name 进行搜索。
性能方面,有什么区别吗?
Assuming the following scenario:
class Project{
public Job Job;
}
class Job{
public Name;
}
Assuming I want to use the Criteria API to search for all projects whose Job has the name "sumthing".
I could use the CreateAlias to create an alias for Job and use it to access Name, or I could create a new Criteria for the property Job and search by Name.
Performance wise, is there any difference?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
考虑到这些要求,没有什么区别,生成的 SQL 是相同的:
对于映射:
和类,
这些条件定义
生成相同的 SQL
注释,但是
CreateAlias
依赖于映射来生成关联,而CreateCriteria
调用允许指定JoinType< /代码>。
所以,这些调用
生成这些 SQL 语句
given these requirements there would be no difference, the generated SQL is the same:
for mappings:
and classes
these criteria definitions
generate the same SQL
note however that the
CreateAlias
relies on the mappings to generate associations whereas theCreateCriteria
call allows to specifyJoinType
.so, these calls
generate these SQL statements
为了解释 NHibernate 2.0 + 中 CreateCriteria 和 CreateAlias 之间的区别,让我们看看以下域模型。
现在,如果您编写以下条件来内部联接这些实体,则
上述条件将不起作用,因为当第一个 CreateCriteria 运行时,它返回“Category”实体,因此当第二个 CreateCriteria 执行时,它不会在“Category”实体中找到属性 ProductStocks,并且查询将失败。
因此,编写此条件的正确方法是,
当第一个 CreateAlias 运行时,它返回“Product”实体,当第二个 CreateCriteria 执行时,它将在“Product”实体中找到 ProductStocks 属性。
那么TSQL就会是这样的。
我希望这个能帮上忙。
To explain the difference between CreateCriteria and CreateAlias in NHibernate 2.0 + lets see the following domain model.
Now if you write following criteria to inner join these entities
The above criteria wont work because when the first CreateCriteria runs it return "Category" entity, therefore when the second CreateCriteria execute it wont find property ProductStocks in the "Category" entity and the query will fail.
So the correct way to write this criteria is
When the first CreateAlias runs it return "Product" entity, when the second CreateCriteria execute it will find property ProductStocks in the "Product" entity.
So the TSQL will be like this.
I hope this will help.
createAlias() 返回原始标准作为结果
createCriteria() 返回使用 createCriteria 构造的新标准,
差异在于当链接方法例如
cr.createAlias().add(Restrictions.ilike("code","abc")) 将为实体添加限制 时
cr.createCriteria("parent","p").add(Restrictions.ilike("code","abc")) 将为其父级添加限制
createAlias() returns original criteria as is result
createCriteria() returns new criteria constructed with createCriteria
difference will be when chaining methods e.g.
cr.createAlias().add(Restrictions.ilike("code","abc")) will add restriction to entity
cr.createCriteria("parent","p").add(Restrictions.ilike("code","abc")) will add restriction to its parent