Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 735317
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T07:28:31+00:00 2026-05-14T07:28:31+00:00

I am trying to create a composite key that mimicks the set of PrimaryKeys

  • 0

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:

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T07:28:32+00:00Added an answer on May 14, 2026 at 7:28 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create a composite key for MYSQL, but i dont know
I'm trying to create a table in SQL Server 2000, that has a composite
I am trying to create a DbSyncForeignKeyConstraint to a table with a composite Primary
I'm trying to create a templated composite control that would work in a similar
I'm trying to add a composite primary key to a class and having a
I am trying to create a composite foreign key relationship/constraint. All tables are empty.
I created the master table with the composite primary key. parent table structure is
I'm trying to create a composite ASP.NET control that let's you build an editable
I am trying to create a relation/table in Oracle that is between two many
I have table as below personalInfo CREATE TABLE personalInfo(userid BIGINT AUTO_INCREMENT PRIMARY KEY) patentInfo

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.