0

我正在使用 FluentNHibernate 并构建一个Customer对象公开多个CustomerProperty对象的系统,基本上是每个客户的键值存储。

public class CustomerMap : ClassMap<Customer> {
    public CustomerMap() {
        Table("Customer");
        Id(x => x.Id);

        Map(x => x.UserName);
        HasMany(x => x.Properties);
    }
}

public class CustomerPropertyMap : ClassMap<CustomerProperty> {
    public CustomerPropertyMap() {
        Table("CustomerProperty");
        CompositeId()
            .KeyReference(x => x.Customer, MapData<Customer>.KeyColumn)
            .KeyProperty(x => x.Key);

        Map(x => x.Value);
    }
}

这可以工作并生成正确的 NHibernate 映射。我已经使用 FluentNHibernate 的内置功能导出了它们。

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
    <class xmlns="urn:nhibernate-mapping-2.2" dynamic-update="true" name="Bazinga.Domain.CustomerProperty, Bazinga.Domain, Version=0.0.0.0, Culture=neutral, PublicKeyToken=108b91ac50cc98b9" table="CustomerProperty">
        <composite-id>
            <key-many-to-one name="Customer" class="Bazinga.Domain.Customer, Bazinga.Domain, Version=0.0.0.0, Culture=neutral, PublicKeyToken=108b91ac50cc98b9">
                <column name="CustomerId" />
            </key-many-to-one>
            <key-property name="Key" type="System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
                <column name="Key" />
            </key-property>
        </composite-id>
        <property name="Value" type="System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
            <column name="Value" />
        </property>
    </class>
</hibernate-mapping>

然而,这种映射会生成一个 sql 查询,这在视觉上并不让我满意。(我可能很迂腐。)NHibernate Profiles 向我展示了以下查询的执行。

SELECT properties0_.CustomerId as CustomerId1_,
       properties0_.[Key]      as Key2_1_,
       properties0_.CustomerId as CustomerId13_0_,
       properties0_.[Key]      as Key2_13_0_,
       properties0_.[Value]    as Value3_13_0_
FROM   CustomerProperty properties0_
WHERE  properties0_.CustomerId = 1337 /* @p0 */

CustomerId对和列有重复引用Key。如何删除这些并仅返回三列 ( CustomerId, Key, Value) 的结果?

4

1 回答 1

0

您是否尝试过使用 .KeyProperty 而不是 .KeyReference 的两个 CompositeId 字段?

于 2013-09-19T17:10:10.273 回答