views:

159

answers:

3

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.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>
+1  A: 

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.

SeeSharpCoder
+1  A: 

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)

Stefan Steinegger
Unfortunately the table is a MySQL native table(used for keeping record of MySQL database users) so I cannot change the schema. Would this still work without changing the schema?
LnDCobra
+1  A: 

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.

Diego Mijelshon
That actually works :O
LnDCobra
Of course it does! I wouldn't lie to you :-D
Diego Mijelshon
Thanks! :D It worked
LnDCobra