c# - NHibernate 返回同一行的 4 个实例,而不是唯一的行

标签 c# nhibernate

背景

我对从这个 NHibernate 查询中得到的结果感到有点困惑:

var result = (List<RatingsLipper>)_session.CreateCriteria<RatingsLipper>()
                      .Add<RatingsLipper>(xx => xx.ShareClassId == shareClassId)
                      .List<RatingsLipper>();

哪里RatingsLipper具有以下映射:

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

    <class mutable="false" name="RatingsLipper" table="LipperRating" schema="offline">

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

        <property name="ShareClassId" column="LipperRating_ShareClassId" />

        <property name="RatingDate" column="LipperRating_RatingDate" />
        <property name="TimePeriod" column="LipperRating_RatingTimePeriodYears" />
        <property name="TotalReturn" column="LipperRating_TotalReturn" />
        <property name="ConsistentReturn" column="LipperRating_ConsistentReturn" />
        <property name="Preservation" column="LipperRating_Preservation" />
        <property name="Expense" column="LipperRating_Expense" />

    </class>
</hibernate-mapping>

查询生成以下 SQL:

SELECT this_.LipperRating_LipperId             ,
       this_.LipperRating_ShareClassId         ,
       this_.LipperRating_RatingDate           ,
       this_.LipperRating_RatingTimePeriodYears,
       this_.LipperRating_TotalReturn          ,
       this_.LipperRating_ConsistentReturn     ,
       this_.LipperRating_Preservation         ,
       this_.LipperRating_Expense              
FROM   offline.LipperRating this_
WHERE  this_.LipperRating_ShareClassId = 19278 /* @p0 */

这又产生了这张表:

LipperRating_LipperId LipperRating_ShareClassId LipperRating_RatingDate LipperRating_RatingTimePeriodYears LipperRating_TotalReturn LipperRating_ConsistentReturn LipperRating_Preservation LipperRating_Expense
--------------------- ------------------------- ----------------------- ---------------------------------- ------------------------ ----------------------------- ------------------------- --------------------
60011179              19278                     2011-02-28 00:00:00.000 3                                  3                        2                             4                         0
60011179              19278                     2011-02-28 00:00:00.000 5                                  3                        3                             5                         0
60011179              19278                     2011-02-28 00:00:00.000 10                                 5                        4                             5                         0
60011179              19278                     2011-02-28 00:00:00.000 99                                 4                        4                             4                         0

问题

我遇到的问题是,我从 NHibernate 查询获得的结果包含第一行的 4 个实例,而不是我们在 SQL 查询的表格输出中看到的 4 个不同结果。

这与表 LipperRating 有关系吗?有一个复合主键?作为引用,此表定义为:

CREATE TABLE [offline].[LipperRating](
    [LipperRating_ShareClassId] [int] NOT NULL,
    [LipperRating_LipperId] [int] NOT NULL,
    [LipperRating_RatingDate] [datetime] NOT NULL,
    [LipperRating_RatingTimePeriodYears] [int] NOT NULL,
    [LipperRating_TotalReturn] [int] NULL,
    [LipperRating_ConsistentReturn] [int] NULL,
    [LipperRating_Preservation] [int] NULL,
    [LipperRating_Expense] [int] NULL,
 CONSTRAINT [LipperRatings_PK] PRIMARY KEY CLUSTERED 
(
    [LipperRating_ShareClassId] ASC,
    [LipperRating_LipperId] ASC,
    [LipperRating_RatingDate] ASC,
    [LipperRating_RatingTimePeriodYears] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

问题

我需要做什么才能将正确的结果输入我的 List<RatingsLipper>

最佳答案

我在以下工具的帮助下弄明白了:http://dotnetslackers.com/Community/blogs/antrad/archive/2008/02/10/how-to-map-composite-key-in-nhibernate.aspx

我需要 <composite-id>

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

    <class mutable="false" name="RatingsLipper" table="LipperRating" schema="offline">
        <composite-id>
            <key-property name="Id" column="LipperRating_LipperId" />
            <key-property name="ShareClassId" column="LipperRating_ShareClassId" />
            <key-property name="RatingDate" column="LipperRating_RatingDate" />
            <key-property name="TimePeriod" column="LipperRating_RatingTimePeriodYears" />
        </composite-id>

        <property name="TotalReturn" column="LipperRating_TotalReturn" />
        <property name="ConsistentReturn" column="LipperRating_ConsistentReturn" />
        <property name="Preservation" column="LipperRating_Preservation" />
        <property name="Expense" column="LipperRating_Expense" />
    </class>
</hibernate-mapping>

而且我还必须将其添加到我的 RatingsLipper 中输入:

public override bool Equals(object obj)
{
    bool equals = false;

    if (null != obj)
    {
        RatingsLipper temp = obj as RatingsLipper;
        if (null != temp)
        {
            equals = (this.ShareClassId == temp.ShareClassId 
                && this.TimePeriod.Equals(temp.TimePeriod) 
                && this.Id.Equals(temp.Id)
                && this.RatingDate.Equals(temp.RatingDate));
        }
    }

    return equals;
}

public override int GetHashCode()
{
    int hash = 1122; // no idea of the significance of this number!

    hash += (this.ShareClassId.GetHashCode());
    hash += (this.TimePeriod.GetHashCode());
    hash += (this.Id.GetHashCode());
    hash += (this.RatingDate.GetHashCode());

    return hash;
}

关于c# - NHibernate 返回同一行的 4 个实例,而不是唯一的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5498501/

相关文章:

c# - 从 Expression<Func<TypeIn, TypeOut1>> 转换为 Expression<Func<TypeIn, TypeOut2>>

NHibernate:用于检索具有空计数子集合的所有实体的条件表达式

c# - 通过参数名解析扩展方法

c# - 如何避免重复条件逻辑?

c# - 反序列化xlink :href

c# - Android 如何以编程方式使线性布局可滚动?

c# - OnTriggerEnter 调用两次

c# - nHibernate,将两个属性映射到同一个类

nhibernate - 使用 NHibernate 和 Mono.Data.SQLite

c# - NHibernate QueryOver 的按位运算和扩展方法