开发者

NHibernate Composite Key

I have created a composite key, and it is working, but ideals I would like the separate directly fields in the row class.

The current way I'm doing this is the following:

    private UserPrimaryKey _compositeKey;
    public virtual UserPrimaryKey CompositeKey
    {
        get
        {
            if (_compositeKey == null) _compositeKey = new UserPrimaryKey();
            return _compositeKey;
        }
        set {
            if (_compositeKey == value) return;
            _compositeKey = value;
            Host = value.Host;
            UserAccount = value.User;
        }
    }
    public string Host { get; set; }
    public string UserAccount { get; set; }

And i was wondering if there is any better way of doing this? Possibly in NHibernate config file.

My current config file is the follwoing:

<class name="TGS.MySQL.DataBaseObjects.DataBasePrivilege,TGS.MySQL.D开发者_如何学PythonataBaseObjects" table="user">
 <composite-id name="CompositeKey" class="TGS.MySQL.DataBaseObjects.UserPrimaryKey, TGS.MySQL.DataBaseObjects">
  <key-property name="Host" column="Host" type="string" length="60" />
  <key-property name="User" column="User" type="string" length="16" />
 </composite-id>
</class>


You can create the properties directly in your class... and map them with:

<composite-id>
  <key-property name="Host"/>
  <key-property name="UserAccount"/>
</composite-id>

If you do that, you'll have to override Equals and GetHashCode in your class.


I would suggest the following:

    private UserPrimaryKey _compositeKey;
    public virtual UserPrimaryKey CompositeKey
    {
        get
        {
            if (_compositeKey == null) _compositeKey = new UserPrimaryKey();
            return _compositeKey;
        }
        set {
            if (_compositeKey == value) return;
            _compositeKey = value;
            Host = value.Host;
            UserAccount = value.User;
        }
    }
    public string Host
    {
        get
        {
            return CompositeKey.Host;
        }
        set
        {
            CompositeKey.Host = value;
        }
    }
    public string UserAccount
    {
        get
        {
            return CompositeKey.User;
        }
        set
        {
            CompositeKey.User = value;
        }
    }

This way you dont duplicate data, only return/set the data inside the composite key.


I would avoid the composite key when ever you can. Replace it with a regular unique constraint on both columns:

<class name="DataBasePrivilege" table="user">
  <id name="id">
    <generator class="hilo">
      <param name="table">user_HiLo</param>
      <param name="max_lo">100</param>
    </generator>
  </id>
  <property name="Host" length="60" unique-key="user_host"/>
  <property name="User" length="16" unique-key="user_host"/>
</class>

(By the way: you don't need to specify types in common cases, and you don't need to specify column names if they match the property names. This keeps you xml readable)

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜