NHibernate映射 [英] NHibernate mapping

查看:132
本文介绍了NHibernate映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用HBM映射. 我有桌子:

I use HBM mapping. I have tables :

I)有专栏的人: 1. ID 2.类型 3.CREATE_DATE 4.UPDATE_DATE

I) person with columns : 1. ID 2. TYPE 3.CREATE_DATE 4.UPDATE_DATE

II)具有列的属性: 1.ID 2.TYPE(在此示例中,人可能是全部类型) 3.名称 4.CREATE_DATE 5.UPDATE_DATE

II) Attribute with columns: 1.ID 2.TYPE(in this example person may be all type) 3.NAME 4.CREATE_DATE 5.UPDATE_DATE

III)带有列的Attribute_VALUE: 1.ID 2.值 4.OBJECT_ID 5.ATTRIBUTE_ID 6.CREATE_DATE 7.UPDATE_DATE

III) Attribute_VALUE with columns: 1.ID 2.VALUE 4.OBJECT_ID 5.ATTRIBUTE_ID 6.CREATE_DATE 7.UPDATE_DATE

person(ID)和Attribute_VALUE(OBJECT_ID)之间存在一对多关系. Attribute(ID)和Attribute_VALUE(ATTRIBUTE_ID)之间存在一对多关系 我需要构建对象PERSON,该对象包含具有name属性的person和dictionary的所有列. 字典包含键-属性值的名称-值的集合.

There is relationship one-to-many between person(ID) and Attribute_VALUE(OBJECT_ID). There is relationship one-to-many between Attribute(ID) and Attribute_VALUE(ATTRIBUTE_ID) I need build object PERSON that contain all columns of person and dictionary with name attribute. The dictionary contain key - name of attribute value- collection of values .

我可以建立合适的HBM吗?

Can I build appropriate HBM ??

推荐答案

简短答案.

长答案:

考虑当您Attributes.Add("foo", "value")时nhibernate如何匹配属性?它必须在数据库中搜索属性 foo (这不是简单的映射,它的逻辑),或者每次添加一个属性都会创建一个新的属性.

consider how should nhibernate match attributes when you Attributes.Add("foo", "value")? it has to search the db for an attribute foo (which is not a simple mapping, its logic) or it would create a new Attribute, everytime you add one.

因此,根据上述架构,您 a)具有某种自定义的onsave代码(我认为这很费力)或 b),您可以更改Person

So given the above schema you either a) have some kind of custom onsave code (which i think is a lot of effort) or b) you change the Person to

class Person
{
    public virtual int Id { get; set; }

    public virtual ICollection<AttributeValue> Attributes { get; set; }

    public virtual IEnumerable<string> GetValues(string attributeName)
    {
        return Attributes
            .Where(attr => attr.Attribute.Name == attributeName)
            .Select(attr => attr.Value);
    }

    public virtual void AddValue(Attribute attribute, string value)
    {
        Attributes.Add(new AttributeValue
        {
            Attribute = attribute,
            Value = value
        });
    }

    public virtual IEnumerable<string> GetAttributeNames()
    {
        return Attributes
            .Select(attr => attr.Attribute.Name);
    }
}

class Attribute
{
    public virtual int Id { get; set; }
    public virtual string Name { get; set; }
    // and more Properties like created and updated
}

class AttributeValue
{
    public virtual int Id { get; set; }

    public virtual Attribute Attribute { get; set; }
    public virtual string Value { get; set; }

    // and more Properties like created and updated
}

然后使用

<class name="Person" table="Persons" xmlns="urn:nhibernate-mapping-2.2">
  <id name="Id" column="ID"/>
  <bag name="Attributes">
    <key column="OBJECT_ID"/>
    <one-to-many class="AttributeValue"/>
  </bag>
</class>

<class name="Attribute" table="Attributes" xmlns="urn:nhibernate-mapping-2.2">
  <id name="Id" column="ID"/>
  <property name="Name" column="Name"/>
  <!--additional properties-->
</class>

<class name="AttributeValue" table="AttributeValues" xmlns="urn:nhibernate-mapping-2.2">
  <id name="Id" column="ID"/>
  <many-to-one class="Attribute" column="ATTRIBUTE_ID"/>
  <property name="Value" column="Value"/>
  <!--additional properties-->
</class>

这篇关于NHibernate映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆