gpt4 book ai didi

c# - 使用 NHibernate Criteria API 选择特定的数据集和计数

转载 作者:太空狗 更新时间:2023-10-29 23:48:51 25 4
gpt4 key购买 nike

我为 NHibernate 的持久性设置了以下域: Domain

我正在使用 PaperConfiguration 作为根聚合。

我想为给定的 Tier 和 AcademicYearConfiguration 选择所有 PaperConfiguration 对象。根据以下示例,这非常有效:

ICriteria criteria =
session.CreateCriteria<PaperConfiguration>()
.Add(Restrictions.Eq("AcademicYearConfiguration", configuration))
.CreateCriteria("Paper")
.CreateCriteria("Unit")
.CreateCriteria("Tier")
.Add(Restrictions.Eq("Id", tier.Id))

return criteria.List<PaperConfiguration>();

(不过也许有更好的方法)。

但还需要知道每个 PaperConfiguration 有多少 ReferenceMaterials,我想在同一个调用中获取它。避免使用 HQL - 我已经有了一个 HQL 解决方案。

我知道这就是预测的目的,this question提出了一个想法,但我无法让它发挥作用。

我有一个 PaperConfigurationView,而不是 IList<ReferenceMaterial> ReferenceMaterials ReferenceMaterialCount 并按照以下思路进行思考

ICriteria criteria =
session.CreateCriteria<PaperConfiguration>()
.Add(Restrictions.Eq("AcademicYearConfiguration", configuration))
.CreateCriteria("Paper")
.CreateCriteria("Unit")
.CreateCriteria("Tier")
.Add(Restrictions.Eq("Id", tier.Id))
.SetProjection(
Projections.ProjectionList()
.Add(Projections.Property("IsSelected"), "IsSelected")
.Add(Projections.Property("Paper"), "Paper")
// and so on for all relevant properties
.Add(Projections.Count("ReferenceMaterials"), "ReferenceMaterialCount")
.SetResultTransformer(Transformers.AliasToBean<PaperConfigurationView>());

return criteria.List< PaperConfigurationView >();

不幸的是,这不起作用。我做错了什么?

以下简化查询:

ICriteria criteria =
session.CreateCriteria<PaperConfiguration>()
.CreateCriteria("ReferenceMaterials")
.SetProjection(
Projections.ProjectionList()
.Add(Projections.Property("Id"), "Id")
.Add(Projections.Count("ReferenceMaterials"), "ReferenceMaterialCount")
).SetResultTransformer(Transformers.AliasToBean<PaperConfigurationView>());
return criteria.List< PaperConfigurationView >();

创建了这个意想不到的 SQL:

SELECT 
this_.Id as y0_,
count(this_.Id) as y1_
FROM Domain.PaperConfiguration this_
inner join Domain.ReferenceMaterial referencem1_
on this_.Id=referencem1_.PaperConfigurationId

上面的查询因 ADO.NET 错误而失败,因为它显然不是正确的 SQL,因为它缺少分组依据或计数是 count(referencem1_.Id) 而不是 (this_.Id)。

NHibernate 映射:

  <class name="PaperConfiguration" table="PaperConfiguration">
<id name="Id" type="Int32">
<column name="Id" sql-type="int" not-null="true" unique="true" index="PK_PaperConfiguration"/>
<generator class="native" />
</id>
<!-- IPersistent -->
<version name="VersionLock" />
<!-- IAuditable -->
<property name="WhenCreated" type="DateTime" />
<property name="CreatedBy" type="String" length="50" />
<property name="WhenChanged" type="DateTime" />
<property name="ChangedBy" type="String" length="50" />

<property name="IsEmeEnabled" type="boolean" not-null="true" />

<property name="IsSelected" type="boolean" not-null="true" />

<many-to-one name="Paper" column="PaperId" class="Paper" not-null="true" access="field.camelcase"/>

<many-to-one name="AcademicYearConfiguration" column="AcademicYearConfigurationId" class="AcademicYearConfiguration" not-null="true" access="field.camelcase"/>

<bag name="ReferenceMaterials" generic="true" cascade="delete" lazy="true" inverse="true">
<key column="PaperConfigurationId" not-null="true" />
<one-to-many class="ReferenceMaterial" />
</bag>
</class>

<class name="ReferenceMaterial" table="ReferenceMaterial">
<id name="Id" type="Int32">
<column name="Id" sql-type="int" not-null="true" unique="true" index="PK_ReferenceMaterial"/>
<generator class="native" />
</id>
<!-- IPersistent -->
<version name="VersionLock" />
<!-- IAuditable -->
<property name="WhenCreated" type="DateTime" />
<property name="CreatedBy" type="String" length="50" />
<property name="WhenChanged" type="DateTime" />
<property name="ChangedBy" type="String" length="50" />

<property name="Name" type="String" not-null="true" />
<property name="ContentFile" type="String" not-null="false" />
<property name="Position" type="int" not-null="false" />
<property name="CommentaryName" type="String" not-null="false" />
<property name="CommentarySubjectTask" type="String" not-null="false" />
<property name="CommentaryPointScore" type="String" not-null="false" />
<property name="CommentaryContentFile" type="String" not-null="false" />

<many-to-one name="PaperConfiguration" column="PaperConfigurationId" class="PaperConfiguration" not-null="true"/>
</class>

最佳答案

您应该使用 Projections.GroupProperty() 而不是 Projections.Property()。

关于c# - 使用 NHibernate Criteria API 选择特定的数据集和计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1488094/

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