NHibernate MySQL Composite-Key
- by LnDCobra
I am trying to create a composite key that mimicks the set of PrimaryKeys in the built in MySQL.DB table.
The Db primary key is as follows:
Field | Type | Null |
----------------------------------
Host | char(60) | No |
Db | char(64) | No |
User | char(16) | No |
This is my DataBasePrivilege.hbm.xml file
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="TGS.MySQL.DataBaseObjects" namespace="TGS.MySQL.DataBaseObjects">
<class name="TGS.MySQL.DataBaseObjects.DataBasePrivilege,TGS.MySQL.DataBaseObjects" table="db">
<composite-id name="CompositeKey" class="TGS.MySQL.DataBaseObjects.DataBasePrivilegePrimaryKey, TGS.MySQL.DataBaseObjects">
<key-property name="Host" column="Host" type="char" length="60" />
<key-property name="DataBase" column="Db" type="char" length="64" />
<key-property name="User" column="User" type="char" length="16" />
</composite-id>
</class>
</hibernate-mapping>
The following are my 2 classes for my composite key:
namespace TGS.MySQL.DataBaseObjects
{
public class DataBasePrivilege
{
public virtual DataBasePrivilegePrimaryKey CompositeKey { get; set; }
}
public class DataBasePrivilegePrimaryKey
{
public string Host { get; set; }
public string DataBase { get; set; }
public string User { get; set; }
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != typeof (DataBasePrivilegePrimaryKey)) return false;
return Equals((DataBasePrivilegePrimaryKey) obj);
}
public bool Equals(DataBasePrivilegePrimaryKey other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return Equals(other.Host, Host) && Equals(other.DataBase, DataBase) && Equals(other.User, User);
}
public override int GetHashCode()
{
unchecked
{
int result = (Host != null ? Host.GetHashCode() : 0);
result = (result*397) ^ (DataBase != null ? DataBase.GetHashCode() : 0);
result = (result*397) ^ (User != null ? User.GetHashCode() : 0);
return result;
}
}
}
}
And the following is the exception I am getting:
Execute
System.InvalidCastException: Unable to cast object of type 'System.Object[]' to type 'TGS.MySQL.DataBaseObjects.DataBasePrivilegePrimaryKey'.
at (Object , GetterCallback )
at NHibernate.Bytecode.Lightweight.AccessOptimizer.GetPropertyValues(Object target)
at NHibernate.Tuple.Component.PocoComponentTuplizer.GetPropertyValues(Object component)
at NHibernate.Type.ComponentType.GetPropertyValues(Object component, EntityMode entityMode)
at NHibernate.Type.ComponentType.GetHashCode(Object x, EntityMode entityMode)
at NHibernate.Type.ComponentType.GetHashCode(Object x, EntityMode entityMode, ISessionFactoryImplementor factory)
at NHibernate.Engine.EntityKey.GenerateHashCode()
at NHibernate.Engine.EntityKey..ctor(Object identifier, String rootEntityName, String entityName, IType identifierType, Boolean batchLoadable, ISessionFactoryImplementor factory, EntityMode entityMode)
at NHibernate.Engine.EntityKey..ctor(Object id, IEntityPersister persister, EntityMode entityMode)
at NHibernate.Event.Default.DefaultLoadEventListener.OnLoad(LoadEvent event, LoadType loadType)
at NHibernate.Impl.SessionImpl.FireLoad(LoadEvent event, LoadType loadType)
at NHibernate.Impl.SessionImpl.Get(String entityName, Object id)
at NHibernate.Impl.SessionImpl.Get(Type entityClass, Object id)
at NHibernate.Impl.SessionImpl.Get[T](Object id)
at TGS.MySQL.DataBase.DataProvider.GetDatabasePrivilegeByHostDbUser(String host, String db, String user) in C:\Documents and Settings\Michal\My Documents\Visual Studio 2008\Projects\TGS\TGS.MySQL.DataBase\DataProvider.cs:line 20
at TGS.UserAccountControl.UserAccountManager.GetDatabasePrivilegeByHostDbUser(String host, String db, String user) in C:\Documents and Settings\Michal\My Documents\Visual Studio 2008\Projects\TGS\TGS.UserAccountControl\UserAccountManager.cs:line 10
at TGS.UserAccountControlTest.UserAccountManagerTest.CanGetDataBasePrivilegeByHostDbUser() in C:\Documents and Settings\Michal\My Documents\Visual Studio 2008\Projects\TGS\TGS.UserAccountControlTest\UserAccountManagerTest.cs:line 12
I am new to NHibernate and any help would be appreciated. I just can't see where it is getting the object[] from? Is the composite key supposed to be object[]?