1

複合キーを作成しましたが、機能していますが、理想的には、行クラスの直接フィールドを分離したいと考えています。

私がこれを行っている現在の方法は次のとおりです。

    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; }

そして、これを行うためのより良い方法があるかどうか疑問に思っていましたか? おそらくNHibernate構成ファイルにあります。

私の現在の設定ファイルは次のとおりです。

<class name="TGS.MySQL.DataBaseObjects.DataBasePrivilege,TGS.MySQL.DataBaseObjects" 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>
4

3 に答える 3

2

クラスでプロパティを直接作成できます...そしてそれらを次のようにマップします:

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

それを行う場合は、クラスでオーバーライドする必要がありEqualsますGetHashCode

于 2010-04-23T20:48:16.700 に答える
1

可能な限り、複合キーは避けます。両方の列で通常の一意性制約に置き換えます。

<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>

(ちなみに、一般的なケースでは型を指定する必要はありません。また、プロパティ名と一致する場合は列名を指定する必要もありません。これにより、xmlが読みやすくなります)

于 2010-04-23T09:34:49.080 に答える
1

次のことをお勧めします。

    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;
        }
    }

この方法では、データを複製せず、複合キー内のデータのみを返す/設定します。

于 2010-04-23T08:14:37.287 に答える