开发者

How to construct an NHibernate query based on the following schema?

I'm trying to construct an NHibernate query to get a list of countries that a Share Class is available for sale (AFS) in.

This is the definition of the AFS table:

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]

and this is the definition of the Country table:

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]

At first, I'm I'm trying to make this query as simple as possible, as follows:

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

This is always returning nothing, even though I know there's (tons of) stuff in the AFS & Country tables, which is suggesting to me that there's something wrong with my mapping files.

This is the AFS mapping:

<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="开发者_StackOverflow中文版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>

and this is the Country 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>

... and with all this, I'm getting the following error message:

The 'type' attribute is not declared. 

Ideally, the result would be an AFS object that contains a List<Country>. Is there something obvious I'm doing wrong?


Okay, here's what I think's wrong...

  1. Your mappings shou;d have the full assembly qualified type name as the name attribtue in the class element.
  2. Your property elements should have the full assembly qualified type name for the type they map to. (this is probably what it's complaining about since it mentions 'type'.

Here's an example from our application...

<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>

Note: this mapping was generated using the Fluent NHibernate API. It might include some optional elements that you don't need but I don't know because I don't do "manual" mapping.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜