-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: identifier type mismatch using Guid
PostPosted: Fri Jun 01, 2007 5:04 pm 
Newbie

Joined: Fri Apr 01, 2005 5:20 pm
Posts: 14
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version: 1.2


Hi I have been trying out the NHibernate 1.2 and at the same time some asp.net stuff. I am trying to use the asp.net membership/login utilities out of the box and then start to use NHibernate for my objects.

    I login with ASP.Net with no problem.
    I then use NHibernate to Get the user object passing in the key from the asp.net user.
    Get(typeOf(aspnetUser), id);
    Exception occurs.


I have never used Guid before and I am not sure if I am doing some thing wrong there.


Exception
Code:
identifier type mismatch
Parameter name: id
   at NHibernate.Engine.EntityKey..ctor(Object id, Object identifierSpace, Type clazz, IType identifierType, Boolean isBatchLoadable, ISessionFactoryImplementor factory)
   at NHibernate.Engine.EntityKey..ctor(Object id, IEntityPersister p)
   at NHibernate.Impl.SessionImpl.DoLoadByClass(Type clazz, Object id, Boolean checkDeleted, Boolean allowProxyCreation)...



Mapping file for the aspnetUser class:
Code:
<?xml version="1.0" encoding="utf-8" ?>
<!-- Generated by ObjectMapper 5/31/2007 11:40:12 AM. http://www.puzzleframework.com -->
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" schema="dbo">
  <class name="Trippin.Data.aspnetUser,Trippin.Data" table="aspnet_Users">
    <id name="Id" type="System.Guid" column="UserId">
      <generator class="assigned" />
    </id>
    <bag name="aspnetRoles" table="aspnet_UsersInRoles" inverse="true">
      <key column="UserId" />
      <many-to-many column="RoleId" class="Trippin.Data.aspnetRole, Trippin.Data" />
    </bag>
    <one-to-one name="aspnetMembership" class="Trippin.Data.aspnetMembership, Trippin.Data" />
    <bag name="aspnetPersonalizationPerUsers" inverse="true">
      <key column="UserId" />
      <one-to-many class="Trippin.Data.aspnetPersonalizationPerUser, Trippin.Data" />
    </bag>
    <one-to-one name="aspnetProfile" class="Trippin.Data.aspnetProfile, Trippin.Data" />
    <many-to-one name="Application" column="ApplicationId" class="Trippin.Data.aspnetApplication, Trippin.Data" />
    <property name="UserName" column="UserName" type="String" />
    <property name="LoweredUserName" column="LoweredUserName" type="String" />
    <property name="MobileAlias" column="MobileAlias" type="String" />
    <property name="IsAnonymous" column="IsAnonymous" type="Boolean" />
    <property name="LastActivityDate" column="LastActivityDate" type="DateTime" />
....


c# file
Code:
using System;

namespace Trippin.Data
{

    public class aspnetUser
    {

        private System.Guid m_Id;
        private System.Collections.Generic.IList<aspnetRole> m_aspnetRoles;
        private aspnetMembership m_aspnetMembership;
        private System.Collections.Generic.IList<aspnetPersonalizationPerUser> m_aspnetPersonalizationPerUsers;
        private aspnetProfile m_aspnetProfile;
        private aspnetApplication m_Application;
        private System.String m_UserName;
        private System.String m_LoweredUserName;
        private System.String m_MobileAlias;
        private System.Boolean m_IsAnonymous;
        private System.DateTime m_LastActivityDate;
        private System.Collections.Generic.IList<Contact> m_Contacts;
        private System.Collections.Generic.IList<Foursome> m_Foursomes;
        private System.Collections.Generic.IList<Invitee> m_Invitees;
        private System.Collections.Generic.IList<Message> m_Messages;
        private System.Collections.Generic.IList<Message> m_ToMessages;
        private System.Collections.Generic.IList<PhotoLink> m_PhotoLinks;
        private System.Collections.Generic.IList<Room> m_Rooms;
        private System.Collections.Generic.IList<Trip> m_Trips;
        private System.Collections.Generic.IList<TripEmail> m_TripEmails;
        private Profile m_Profile;

         public virtual System.Guid Id
        {
            get
            {
                return m_Id;
            }
            set
            {
                m_Id = value;
            }
        }

         public virtual System.Collections.Generic.IList<aspnetRole> aspnetRoles
        {
            get
            {
                return m_aspnetRoles;
            }
            set
            {
                m_aspnetRoles = value;
            }
        }


apsx.cs code that makes the call
Code:
       protected void LoginName1_Load(object sender, EventArgs e)
        {
            try
            {
                MembershipUser user = Membership.GetUser();
                if (user != null)
                {
                    HibernateDataAccess hda = new HibernateDataAccess();

                    System.Guid id = (System.Guid)user.ProviderUserKey;

                    log.Debug("The user id is <" + id + ">");
                    Trippin.Data.aspnetUser trippinUser = (Trippin.Data.aspnetUser)hda.get(typeof(Trippin.Data.aspnetUser),id);


DataAccessClass
Code:
using NHibernate;
using log4net;

namespace Trippin.DataAccess
{
    public class HibernateDataAccess
    {
        ISession m_session;
        ILog log = LogManager.GetLogger("HibernateDataAccess");
       
        public HibernateDataAccess()
        {
            m_session = NHibernateHelper.GetCurrentSession();
        }

        public Object get(Type t, object o_id)
        {
            try
            {

                log.Debug("The type is " + t.ToString());
                log.Debug("The id is " + o_id);

               
                return m_session.Get(t, o_id);
            }
            catch (Exception ex)
            {
                log.Error("Exception getting object ", ex);
               
                return null;
            }
        }


Session handler
Code:
using NHibernate;
using NHibernate.Cfg;
using log4net;

namespace Trippin.DataAccess
{
    public sealed class NHibernateHelper
    {
        private const string CurrentSessionKey = "nhibernate.current_session";
        private static readonly ISessionFactory sessionFactory;
        static ILog log = LogManager.GetLogger("NHibernateHelper");

        static NHibernateHelper()
        {
            try
            {
                log.Debug("Initializing NHibernate SessionFactory...");

                NHibernate.Cfg.Environment.UseReflectionOptimizer = false;
                NHibernate.Cfg.Configuration config = new NHibernate.Cfg.Configuration();
                config.Configure();
                sessionFactory = config.BuildSessionFactory();
            }
            catch (Exception ex)
            {
                log.Error("Exception creating the factory ", ex);
            }

            //sessionFactory = new NHibernate.Cfg.Configuration().Configure().BuildSessionFactory();
        }

        public static ISession GetCurrentSession()
        {
            HttpContext context = HttpContext.Current;
            ISession currentSession = context.Items[CurrentSessionKey] as ISession;

            if (currentSession == null)
            {
                currentSession = sessionFactory.OpenSession();
                context.Items[CurrentSessionKey] = currentSession;
            }

            return currentSession;
        }

        public static void CloseSession()
        {
            HttpContext context = HttpContext.Current;
            ISession currentSession = context.Items[CurrentSessionKey] as ISession;

            if (currentSession == null)
            {
                // No current session
                return;
            }

            currentSession.Close();
            context.Items.Remove(CurrentSessionKey);
        }

        public static void CloseSessionFactory()
        {
            if (sessionFactory != null)
            {
                sessionFactory.Close();
            }
        }
    }
}


sql generated
Code:
[SQL: SELECT aspnetuser0_.UserId as UserId21_2_, aspnetuser0_.ApplicationId as Applicat2_21_2_, aspnetuser0_.UserName as UserName21_2_, aspnetuser0_.LoweredUserName as LoweredU4_21_2_, aspnetuser0_.MobileAlias as MobileAl5_21_2_, aspnetuser0_.IsAnonymous as IsAnonym6_21_2_, aspnetuser0_.LastActivityDate as LastActi7_21_2_, aspnetmemb1_.UserId as UserId15_0_, aspnetmemb1_.ApplicationId as Applicat2_15_0_, aspnetmemb1_.Password as Password15_0_, aspnetmemb1_.PasswordFormat as Password4_15_0_, aspnetmemb1_.PasswordSalt as Password5_15_0_, aspnetmemb1_.MobilePIN as MobilePIN15_0_, aspnetmemb1_.Email as Email15_0_, aspnetmemb1_.LoweredEmail as LoweredE8_15_0_, aspnetmemb1_.PasswordQuestion as Password9_15_0_, aspnetmemb1_.PasswordAnswer as Passwor10_15_0_, aspnetmemb1_.IsApproved as IsApproved15_0_, aspnetmemb1_.IsLockedOut as IsLocke12_15_0_, aspnetmemb1_.CreateDate as CreateDate15_0_, aspnetmemb1_.LastLoginDate as LastLog14_15_0_, aspnetmemb1_.LastPasswordChangedDate as LastPas15_15_0_, aspnetmemb1_.LastLockoutDate as LastLoc16_15_0_, aspnetmemb1_.FailedPasswordAttemptCount as FailedP17_15_0_, aspnetmemb1_.FailedPasswordAttemptWindowStart as FailedP18_15_0_, aspnetmemb1_.FailedPasswordAnswerAttemptCount as FailedP19_15_0_, aspnetmemb1_.FailedPasswordAnswerAttemptWindowStart as FailedP20_15_0_, aspnetmemb1_.Comment as Comment15_0_, aspnetprof2_.UserId as UserId13_1_, aspnetprof2_.PropertyNames as Property2_13_1_, aspnetprof2_.PropertyValuesString as Property3_13_1_, aspnetprof2_.PropertyValuesBinary as Property4_13_1_, aspnetprof2_.LastUpdatedDate as LastUpda5_13_1_ FROM dbo.aspnet_Users aspnetuser0_ left outer join Trippin.dbo.aspnet_Membership aspnetmemb1_ on aspnetuser0_.UserId=aspnetmemb1_.UserId left outer join aspnet_Profile aspnetprof2_ on aspnetuser0_.UserId=aspnetprof2_.UserId WHERE aspnetuser0_.UserId=?]


Sorry for the long post and thanks for any help.

_________________
Thanks,

John


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.