带有鉴别器的每个子类的Nhibernate表

时间:2012-09-19 09:48:05

标签: c# nhibernate inheritance nhibernate-mapping

我遇到了一个问题,由于某种未知的原因,NHibernate正在生成错误的SQL。

以下是我的映射文件:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   assembly="BAM.PMT.Domain"
                   namespace="BAM.PMT.Domain.Core.Cos">

    <class name="SiteServiceEntity" table="SiteServiceEntity" schema="core_cos" dynamic-update="true" abstract="true" discriminator-value="-1">
        <id name="Id" access="field.camelcase-underscore">
            <generator class="guid.comb" />
        </id>

        <discriminator column="Type" type="short" />

        <component name="Name" access="field.camelcase-underscore">
            <property name="Nl" column="NameNl" />
            <property name="Fr" column="NameFr" />
            <property name="En" column="NameEn" />
            <property name="De" column="NameDe" />
        </component>

        <property name="Code" access="field.camelcase-underscore" />

        <subclass name="BAM.PMT.Domain.Core.Cos.SiteServiceCategory" dynamic-update="true" discriminator-value="0">
            <set name="SiteServiceGroups" table="SiteServiceGroup" schema="core_cos" cascade="none" inverse="true" access="field.camelcase-underscore">
                <key column="SiteServiceCategoryId" />
                <one-to-many class="SiteServiceGroup" />
            </set>
            <join table="SiteServiceCategory" schema="core_cos">
                <key column="Id"/>

                <many-to-one name="Site" class="BAM.PMT.Domain.Core.Sites.Site" column="SiteId" cascade="none" access="field.camelcase-underscore" />
            </join>
        </subclass>

        <subclass name="BAM.PMT.Domain.Core.Cos.SiteServiceGroup" dynamic-update="true" discriminator-value="1">
            <set name="SiteServices" table="SiteService" schema="core_cos" cascade="none" inverse="true" access="field.camelcase-underscore">
                <key column="SiteServiceGroupId" />
                <one-to-many class="SiteService" />
            </set>
            <join table="SiteServiceGroup" schema="core_cos">
                <key column="Id"/>

                <many-to-one name="SiteServiceCategory" class="SiteServiceCategory" column="SiteServiceCategoryId" cascade="none" access="field.camelcase-underscore" />

            </join>
        </subclass>

        <subclass name="BAM.PMT.Domain.Core.Cos.SiteService" dynamic-update="true" discriminator-value="2">
            <set name="Subcontractors" table="SiteServiceSubcontractor" schema="core_cos" cascade="save-update" access="field.camelcase-underscore">
                <key column="SiteServiceId" />
                <many-to-many class="BAM.PMT.Domain.Core.Sites.Subcontractor" column="SubcontractorId" />
            </set>

            <set name="SiteKpis" table="SiteKPI" schema="core_cos" cascade="none" inverse="true" access="field.camelcase-underscore">
                <key column="SiteServiceId" />
                <one-to-many class="SiteKpi" />
            </set>

            <set name="SiteCauses" table="SiteCause" schema="helpdesk" cascade="none" inverse="true" access="field.camelcase-underscore">
                <key column="SiteServiceId" />
                <one-to-many class="BAM.PMT.Domain.Helpdesk.SiteCause" />
            </set>
            <join table="SiteService" schema="core_cos">
                <key column="Id"/>

                <many-to-one name="SiteServiceGroup" class="SiteServiceGroup" column="SiteServiceGroupId" cascade="none" access="field.camelcase-underscore" />
                <many-to-one name="Service" class="Service" column="ServiceId" cascade="none" access="field.camelcase-underscore" />
                <many-to-one name="SiteResponsible" class="BAM.PMT.Domain.Core.Security.User" column="SiteResponsibleId" cascade="none" access="field.camelcase-underscore" />
                <many-to-one name="BamResponsible" class="BAM.PMT.Domain.Core.Security.User" column="BamResponsibleId" cascade="none" access="field.camelcase-underscore" />
                <many-to-one name="SubcontractorResponsible" class="BAM.PMT.Domain.Core.Security.User" column="SubContractorResponsibleId" cascade="none" access="field.camelcase-underscore" />

            </join>
        </subclass>
    </class>

</hibernate-mapping>

当我使用以下内容查询完整树结构时:

var query = QueryOver.Of<SiteServiceCategory>();
            query.OrderBy(s => s.Code).Asc()
                .ThenBy(s => s.Id).Asc()
                .Left.JoinQueryOver(sc => sc.SiteServiceGroups)
                .OrderBy(sg => sg.Code).Asc()
                .ThenBy(sg => sg.Id).Asc()
                .Left.JoinQueryOver(sg => sg.SiteServices)
                .OrderBy(s => s.Code).Asc()
                .ThenBy(s => s.Id).Asc()
                .Left.JoinQueryOver(s => s.Service);

            query
                .Where(sc => sc.Site.Id == siteId)
                .TransformUsing(Transformers.DistinctRootEntity);



            return query;

生成以下SQL:

SELECT this_.id                                  AS Id105_3_,
       this_.namenl                              AS NameNl105_3_,
       this_.namefr                              AS NameFr105_3_,
       this_.nameen                              AS NameEn105_3_,
       this_.namede                              AS NameDe105_3_,
       this_.code                                AS Code105_3_,
       this_1_.siteid                            AS SiteId106_3_,
       siteservic1_1_.siteservicecategoryid      AS SiteServ2_5_,
       siteservic1_.id                           AS Id5_,
       siteservic1_.id                           AS Id105_0_,
       siteservic1_.namenl                       AS NameNl105_0_,
       siteservic1_.namefr                       AS NameFr105_0_,
       siteservic1_.nameen                       AS NameEn105_0_,
       siteservic1_.namede                       AS NameDe105_0_,
       siteservic1_.code                         AS Code105_0_,
       siteservic1_1_.siteservicecategoryid      AS SiteServ2_107_0_,
       siteservic2_1_.siteservicegroupid         AS SiteServ2_6_,
       siteservic2_.id                           AS Id6_,
       siteservic2_.id                           AS Id105_1_,
       siteservic2_.namenl                       AS NameNl105_1_,
       siteservic2_.namefr                       AS NameFr105_1_,
       siteservic2_.nameen                       AS NameEn105_1_,
       siteservic2_.namede                       AS NameDe105_1_,
       siteservic2_.code                         AS Code105_1_,
       siteservic2_1_.siteservicegroupid         AS SiteServ2_109_1_,
       siteservic2_1_.serviceid                  AS ServiceId109_1_,
       siteservic2_1_.siteresponsibleid          AS SiteResp4_109_1_,
       siteservic2_1_.bamresponsibleid           AS BamRespo5_109_1_,
       siteservic2_1_.subcontractorresponsibleid AS SubContr6_109_1_,
       service3_.id                              AS Id16_2_,
       service3_.code                            AS Code16_2_,
       service3_.namenl                          AS NameNl16_2_,
       service3_.namefr                          AS NameFr16_2_,
       service3_.nameen                          AS NameEn16_2_,
       service3_.namede                          AS NameDe16_2_,
       service3_.isactive                        AS IsActive16_2_,
       service3_.servicegroupid                  AS ServiceG8_16_2_
FROM   core_cos.siteserviceentity this_
       INNER JOIN core_cos.siteservicecategory this_1_
               ON this_.id = this_1_.id
       LEFT OUTER JOIN core_cos.siteserviceentity siteservic1_
                    ON this_.id = siteservic1_.siteservicecategoryid
       LEFT OUTER JOIN core_cos.siteservicegroup siteservic1_1_
                    ON siteservic1_.id = siteservic1_1_.id
       LEFT OUTER JOIN core_cos.siteserviceentity siteservic2_
                    ON siteservic1_.id = siteservic2_.siteservicegroupid
       LEFT OUTER JOIN core_cos.siteservice siteservic2_1_
                    ON siteservic2_.id = siteservic2_1_.id
       LEFT OUTER JOIN core_cos.service service3_
                    ON siteservic2_1_.serviceid = service3_.id
WHERE  this_.type = 0
       AND this_1_.siteid = @p0
ORDER  BY this_.code ASC,
          this_.id ASC,
          siteservic1_.code ASC,
          siteservic1_.id ASC,
          siteservic2_.code ASC,
          siteservic2_.id ASC 

问题在于left outer join siteservic1_.siteservicecategoryidsiteservic2_.siteservicegroupid在这些表格中不存在。这些内容包括以下内容:siteservic1_1_.siteservicecategoryid - siteservic2_1_.siteservicegroupid

任何人都知道发生了什么,如果需要可以提供额外的信息。

提前致谢!

1 个答案:

答案 0 :(得分:-3)

Lazy loading inheritance

Mapping inheritance

这两个解决了我的问题。