我正在尝试创建一个组合键,该组合键模仿内置MySQL.DB表中的PrimaryKeys集。

Db主键如下:

Field  |    Type    |  Null |
----------------------------------
Host   |  char(60)  |   No  |
Db     |  char(64)  |   No  |
User   |  char(16)  |   No  |


这是我的DataBasePrivilege.hbm.xml文件

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


以下是我的复合键的2个类:

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


以下是我得到的例外:

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


编辑:现在我得到此异常。有人可以帮忙吗?

最佳答案

好的,终于可以了。

第一次不起作用,因为我在TestMethod中创建了一个Object [](与NHibernate无关)

第二个例外不起作用,因为我必须将所有字段的所有数据类型都更改为String而不是bool。

关于c# - NHibernate MySQL复合键,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/2677347/

10-09 00:42