gpt4 book ai didi

performance - NHibernate - CreateCriteria 与 CreateAlias

转载 作者:行者123 更新时间:2023-12-03 10:08:23 24 4
gpt4 key购买 nike

假设以下场景:

class Project{
public Job Job;
}

class Job{
public Name;
}

假设我想使用 Criteria API 来搜索 Job 名称为“sumthing”的所有项目。

我可以使用 CreateAlias 为 Job 创建别名并使用它来访问 Name,或者我可以为属性 Job 创建新的 Criteria 并按 Name 搜索。

性能方面,有什么区别吗?

最佳答案

鉴于这些要求没有区别,生成的 SQL 是相同的:
对于映射:

    <?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="Project" table="Project">
<id name="Id" type="Int32" unsaved-value="0">
<column name="Id" sql-type="int" not-null="true" unique="true"/>
<generator class="native" />
</id>
<many-to-one name="Job" column="FK_JobId" cascade="save-update" not-null="true" />
</class>
</hibernate-mapping>
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="Job" table="Job">
<id name="Id" type="Int32" unsaved-value="0">
<column name="Id" sql-type="int" not-null="true" unique="true"/>
<generator class="native" />
</id>
<property name="Name" type="String">
<column name="Name" sql-type="nvarchar" length="50" not-null="true"/>
</property>
</class>
</hibernate-mapping>

和类(class)
public class Project
{
public Project() { }

public virtual int Id { get; set; }

public virtual Job Job { get; set; }
}
public class Job
{
public Job() { }

public virtual int Id { get; set; }

public virtual String Name { get; set; }
}

这些标准定义
ICriteria criteriacrit = session
.CreateCriteria(typeof (Project))
.CreateCriteria("Job", "job")
.Add(Restrictions.Eq("job.Name", "sometextA"));

ICriteria aliascrit = session
.CreateCriteria(typeof (Project))
.CreateAlias("Job", "job")
.Add(Restrictions.Eq("job.Name", "sometextB"));

生成相同的 SQL
SELECT 
this_.Id as Id2_1_,
this_.FK_JobId as FK2_2_1_,
job1_.Id as Id1_0_,
job1_.Name as Name1_0_
FROM
Project this_
inner join Job job1_
on this_.FK_JobId=job1_.Id
WHERE job1_.Name = @p0; @p0 = 'sometextA'

SELECT
this_.Id as Id2_1_,
this_.FK_JobId as FK2_2_1_,
job1_.Id as Id1_0_,
job1_.Name as Name1_0_
FROM
Project this_
inner join Job job1_
on this_.FK_JobId=job1_.Id
WHERE job1_.Name = @p0; @p0 = 'sometextB'

但是请注意 CreateAlias依赖于映射来生成关联,而 CreateCriteria call 允许指定 JoinType .

所以,这些电话
ICriteria criteriacrit = session
.CreateCriteria(typeof(Project))
.CreateCriteria("Job",JoinType.LeftOuterJoin)
.Add(Restrictions.Eq("Name", "sometextA"));

ICriteria aliascrit = session
.CreateCriteria(typeof (Project))
.CreateAlias("Job", "job")
.Add(Restrictions.Eq("job.Name", "sometextB"));

生成这些 SQL 语句
SELECT 
this_.Id as Id2_1_,
this_.FK_JobId as FK2_2_1_,
job1_.Id as Id1_0_,
job1_.Name as Name1_0_
FROM
Project this_
**left outer** join Job job1_
on this_.FK_JobId=job1_.Id
WHERE job1_.Name = @p0; @p0 = 'sometextA'

SELECT
this_.Id as Id2_1_,
this_.FK_JobId as FK2_2_1_,
job1_.Id as Id1_0_,
job1_.Name as Name1_0_
FROM Project this_
**inner join** Job job1_
on this_.FK_JobId=job1_.Id
WHERE job1_.Name = @p0; @p0 = 'sometextB'

关于performance - NHibernate - CreateCriteria 与 CreateAlias,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/899079/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com