如何基于以下模式构建NHibernate查询?

时间:2011-05-17 10:28:14

标签: c# nhibernate

我正在尝试构建一个NHibernate查询,以获取Share Class可供销售(AFS)的国家/地区列表。

这是AFS表的定义:

CREATE TABLE [MStar].[AFS](
    [AFS_ShareClassId] [int] NOT NULL,
    [AFS_CountryId] [int] NOT NULL,
 CONSTRAINT [PK_AFS] PRIMARY KEY CLUSTERED 
(
    [AFS_ShareClassId] ASC,
    [AFS_CountryId] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

ALTER TABLE [MStar].[AFS]  WITH CHECK ADD  CONSTRAINT [FK_AFS_Country] FOREIGN KEY([AFS_CountryId])
REFERENCES [MStar].[Country] ([Country_Id])
GO

ALTER TABLE [MStar].[AFS] CHECK CONSTRAINT [FK_AFS_Country]

这是Country表的定义:

CREATE TABLE [MStar].[Country](
    [Country_Id] [int] NOT NULL,
    [Country_ShortCode] [char](2) NULL,
    [Country_LongCode] [char](3) NOT NULL,
    [Country_RegionId] [char](3) NOT NULL,
    [Country_Name] [varchar](128) NOT NULL,
 CONSTRAINT [PK_Country] PRIMARY KEY CLUSTERED 
(
    [Country_Id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO

ALTER TABLE [MStar].[Country]  WITH CHECK ADD  CONSTRAINT [FK_Country_Region] FOREIGN KEY([Country_RegionId])
REFERENCES [MStar].[Region] ([Region_Id])
GO

ALTER TABLE [MStar].[Country] CHECK CONSTRAINT [FK_Country_Region]

首先,我正在努力使这个查询尽可能简单,如下所示:

var afs = _session.CreateCriteria<AFS>().List<AFS>();

即使我知道AFS和AFS中有(吨)的东西,它总是不返回任何东西。国家表格,向我建议我的地图文件有问题。

这是AFS映射:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   assembly="FTMS.Domain"
                   namespace="FTMS.Domain.Entities">

    <class mutable="false" name="AFS" table="AFS" schema="MStar">
        <composite-id >
            <key-many-to-one name="ShareClass" column="AFS_ShareClassId" type="FTMS.Domain.Entities.ShareClass"></key-many-to-one>
        <key-many-to-one name="Country" column="AFS_CountryId" type="FTMS.Domain.Entities.Country"></key-many-to-one>
        </composite-id>

        <set name="Countries"
             inverse="true"
             lazy="true"
             cascade="save-update">
            <key column="Country_Id"></key>
            <one-to-many class="Country"/>
        </set>
    </class>
</hibernate-mapping>

这是国家地图:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   assembly="FTMS.Domain"
                   namespace="FTMS.Domain.Entities">


  <class mutable="false" name="Country" table="Country" schema="MStar">

    <id name="Id" column="Country_Id">
      <generator class="native"></generator>
    </id>

    <property name="ShortCode" column="Country_ShortCode" />
    <property name="LongCode" column="Country_LongCode" />
    <property name="Name" column="Country_Name" />

      <set name="Funds" lazy="true">
            <key column="Fund_DomicileId"></key>
            <one-to-many class="Fund"/>
      </set>
  </class>
</hibernate-mapping>

...而且有了这一切,我收到以下错误消息:

The 'type' attribute is not declared. 

理想情况下,结果将是包含List<Country>的AFS对象。有什么明显的我做错了吗?

1 个答案:

答案 0 :(得分:0)

好的,这就是我认为的错误......

  1. 你的映射是什么; d有完整的程序集限定类型名称作为类元素中的名称attribtue。
  2. 您的属性元素应具有其映射到的类型的完整程序集限定类型名称。 (这可能是它所抱怨的,因为它提到'类型'。
  3. 以下是我们申请中的一个例子......

    <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-access="property" auto-import="true" default-cascade="none" default-lazy="true">
      <class xmlns="urn:nhibernate-mapping-2.2" mutable="false" name="Civica.Common.Services.Web.Authorization.Model.Operation, Civica.Common.Services.Web, Version=1.0.4140.18095, Culture=neutral, PublicKeyToken=537c3450b3658434" table="`Operation`">
        <id name="Id" type="System.Int64, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
          <column name="Id" />
          <generator class="hilo">
            <param name="max_lo">10000</param>
          </generator>
        </id>
        <property name="Name" type="System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
          <column name="Name" length="50" unique="true" />
        </property>
        <property name="TypeName" type="System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
          <column name="TypeName" length="255" unique="true" />
        </property>
        <property name="Usage" type="System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
          <column name="Usage" length="255" not-null="false" />
        </property>
        <property name="GroupingName" type="System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
          <column name="GroupingName" length="50" not-null="false" />
        </property>
      </class>
    </hibernate-mapping>
    

    注意:此映射是使用Fluent NHibernate API生成的。它可能包含一些您不需要的可选元素,但我不知道,因为我不进行“手动”映射。