NHibernate MySQL Composite-Key
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) && Eq开发者_如何学编程uals(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:
NHibernate.Exceptions.GenericADOException: could not load an entity: [TGS.MySQL.DataBaseObjects.DataBasePrivilege#component[Host,DataBase,User]{'Host'='%', 'DataBase'='totalglobalsteel', 'User'='guy'}][SQL: SELECT databasepr0_.Host as Host0_0_, databasepr0_.Db as Db0_0_, databasepr0_.User as User0_0_ FROM db databasepr0_ WHERE databasepr0_.Host=? and databasepr0_.Db=? and databasepr0_.User=?] ---> System.InvalidCastException: Specified cast is not valid.
at NHibernate.Type.AbstractCharType.Set(IDbCommand cmd, Object value, Int32 index)
at NHibernate.Type.NullableType.NullSafeSet(IDbCommand cmd, Object value, Int32 index)
at NHibernate.Type.NullableType.NullSafeSet(IDbCommand st, Object value, Int32 index, ISessionImplementor session)
at NHibernate.Type.ComponentType.NullSafeSet(IDbCommand st, Object value, Int32 begin, ISessionImplementor session)
at NHibernate.Engine.QueryParameters.BindParameters(IDbCommand command, GetNamedParameterLocations getNamedParameterLocations, Int32 start, ISessionImplementor session)
at NHibernate.Loader.Loader.BindParameterValues(IDbCommand statement, QueryParameters queryParameters, Int32 startIndex, ISessionImplementor session)
at NHibernate.Loader.Loader.PrepareQueryCommand(QueryParameters queryParameters, Boolean scroll, ISessionImplementor session)
at NHibernate.Loader.Loader.DoQuery(ISessionImplementor session, QueryParameters queryParameters, Boolean returnProxies)
at NHibernate.Loader.Loader.DoQueryAndInitializeNonLazyCollections(ISessionImplementor session, QueryParameters queryParameters, Boolean returnProxies)
at NHibernate.Loader.Loader.LoadEntity(ISessionImplementor session, Object id, IType identifierType, Object optionalObject, String optionalEntityName, Object optionalIdentifier, IEntityPersister persister)
--- End of inner exception stack trace ---
at NHibernate.Loader.Loader.LoadEntity(ISessionImplementor session, Object id, IType identifierType, Object optionalObject, String optionalEntityName, Object optionalIdentifier, IEntityPersister persister)
at NHibernate.Loader.Entity.AbstractEntityLoader.Load(ISessionImplementor session, Object id, Object optionalObject, Object optionalId)
at NHibernate.Loader.Entity.AbstractEntityLoader.Load(Object id, Object optionalObject, ISessionImplementor session)
at NHibernate.Persister.Entity.AbstractEntityPersister.Load(Object id, Object optionalObject, LockMode lockMode, ISessionImplementor session)
at NHibernate.Event.Default.DefaultLoadEventListener.LoadFromDatasource(LoadEvent event, IEntityPersister persister, EntityKey keyToLoad, LoadType options)
at NHibernate.Event.Default.DefaultLoadEventListener.DoLoad(LoadEvent event, IEntityPersister persister, EntityKey keyToLoad, LoadType options)
at NHibernate.Event.Default.DefaultLoadEventListener.Load(LoadEvent event, IEntityPersister persister, EntityKey keyToLoad, LoadType options)
at NHibernate.Event.Default.DefaultLoadEventListener.ProxyOrLoad(LoadEvent event, IEntityPersister persister, EntityKey keyToLoad, LoadType options)
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 21
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
Edit: Now I am getting this exception. Could anyone help?
Ok finally got it working.
First time it wasn't working because I was creating an Object[] in my TestMethod (nothing to do with NHibernate)
Second exception wasn't working because I had to change all the data types of all fields to String rather then bool.
精彩评论