![](/img/trans.png)
[英]How to pass schema name in a variable to nHibernate's named query?
[英]How to construct an NHibernate query based on the following schema?
我正在嘗試構造NHibernate查詢,以獲取可供出售的股票類別(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]
這是“國家/地區”表的定義:
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映射:
<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對象。 我做錯了什么嗎?
好的,這就是我認為的錯誤...
這是我們應用程序中的一個示例...
<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生成的。 它可能包括一些不需要的可選元素,但我不知道,因為我不執行“手動”映射。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.