-->
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.  [ 9 posts ] 
Author Message
 Post subject: Unable to cast object help!!
PostPosted: Tue Jan 12, 2010 10:59 am 
Newbie

Joined: Tue Jul 28, 2009 8:28 am
Posts: 18
Hi, could somebody please review my source and let me know where I am going wrong. I am trying to insert a new application record and am receiving an exception when I try session.Flush().

Here is the application mapping (have changed some values for display purposes):

Code:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"  assembly="..." namespace="...">
<class name="Application" table="APPLICATION">
   <id name="UniqueId" column="COUNTER">
      <generator class="sequence">
            <param name="sequence">MySQ</param>
      </generator>
   </id>   
   <property name="CreatedDate" column="CREATE_DATE"/>
   <property name="CreatedBy" column="CREATE_BY" type="AnsiString"/>
   ...
   <many-to-one name="ApplicationType" column="APPLICATION_CODE" class="ApplicationTypeCode" fetch="join" lazy="false" property-ref="CodeValue"/>
   <many-to-one name="ResultCode" column="RESULT_CODE" class="ApplicationResultCode" fetch="join" lazy="false" property-ref="CodeValue"/>
   <property name="ApplicationCost" column="COST" type="AnsiString"/>
</class>
</hibernate-mapping>



Here is the class for application:

Code:
using System;
using System.Collections.Generic;
using System.Text;

namespace ...
{
    public class Application: ICreation
    {
        private int? mUniqueID;

        public virtual int? UniqueId
        {
            get { return mUniqueID; }
            set { mUniqueID = value; }
        }
       
        private DateTime? createdDate;
        private string createdBy;

        public virtual string CreatedBy
        {
            get { return createdBy; }
            set { createdBy = value; }
        }

        public virtual DateTime? CreatedDate
        {
            get { return createdDate; }
            set { createdDate = value; }
        }

        private ApplicationTypeCode mApplicationType;

        public virtual ApplicationTypeCode ApplicationType
        {
            get { return mApplicationType; }
            set { mApplicationType = value; }
        }

        private ApplicationResultCode mResultCode;

        public virtual ApplicationResultCode ResultCode
        {
            get { return mResultCode; }
            set { mResultCode = value; }
        }

        private string mApplicationCost;

        /// <summary>
        /// Gets or sets the application cost, populated from the application type code
        /// </summary>
        /// <value>The application cost.</value>
        public virtual string ApplicationCost
        {
            get { return mApplicationCost; }
            set { mApplicationCost = value; }
        }
       
    }
}



Here is the class for ICreation:

Code:
namespace ...
{
    public interface ICreation
    {
        string CreatedBy
        {
            get;
            set;
        }

        Nullable<DateTime> CreatedDate
        {
            get;
            set;
        }
    }
}



Here is the mapping for codes:

Code:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"  assembly="..." namespace="...">
<class name="CodeListItem" table="CODES" discriminator-value="0">
<cache usage="read-only"/>
   <composite-id >
      <key-property name="CodeListId" column="APP_DATA_CODE"/>
      <key-property name="CodeValue" column="APP_CODE"/>
   </composite-id>
   <discriminator column="APP_DATA_CODE" type="Int32"/>
   <property name="CodeValue" column="APP_CODE" type="AnsiString"/>
   <property name="CodeDescription" column="CODE_DESC" type="AnsiString"/>   
   <property name="CodeListId" column="APP_DATA_CODE" type="AnsiString"/>
</class>
<subclass name="ApplicationTypeCode" discriminator-value="225" extends="CodeListItem">
<property name="CodeValue" column="APP_CODE" type="AnsiString"/>
<property name="ApplicationCost" column="PARENT_APP_CODE" type="AnsiString"/>
<property name="ValidFor" column="APP_TEXT" type="AnsiString"/> 
</subclass>
<subclass name="ApplicationResultCode" discriminator-value="325" extends="CodeListItem">
<property name="CodeValue" column="APP_CODE" type="AnsiString"/>
</subclass>
</hibernate-mapping>





Now the error I get when I try a session.Flush is the following:

Unable to cast object of type 'System.String' to type '........BusinessEntities.CodeListItem'

(replaced the namespaces etc with dots.


Top
 Profile  
 
 Post subject: Re: Unable to cast object help!!
PostPosted: Wed Jan 13, 2010 5:13 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
I'm not sure, what exactly causes this, but at least I see some problems in your mapping. You have duplicate mappings for CodeValue, which probably causes the error. Why do you map the property separately ? The key should be enough.

_________________
--Wolfgang


Top
 Profile  
 
 Post subject: Re: Unable to cast object help!!
PostPosted: Wed Jan 13, 2010 5:26 am 
Newbie

Joined: Tue Jul 28, 2009 8:28 am
Posts: 18
It is mapped again as the ApplicationTypeCode class is a subclass of CodeListItem and it extends it. This mapping file worked successfully in Visual Studio 2005 using the previous release of NHibernate (1.0.2 I think). We then upgraded to Visual Studio 2008 and NHibernate 1.2 and this is when we are getting the problem


Top
 Profile  
 
 Post subject: Re: Unable to cast object help!!
PostPosted: Wed Jan 13, 2010 5:30 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Try and remove the property mappings for CodeValue. In my opinion they are not necessary (except maybe the one in the base class for the property-ref). Is the CodeValue unique ? If not I wonder how the many-to-one is working at all.

_________________
--Wolfgang


Top
 Profile  
 
 Post subject: Re: Unable to cast object help!!
PostPosted: Wed Jan 13, 2010 6:56 am 
Newbie

Joined: Tue Jul 28, 2009 8:28 am
Posts: 18
Hi thanks but that didn't work, am still getting the same error at session.Flush() using the following:

session.Save(application);
session.Flush();


Top
 Profile  
 
 Post subject: Re: Unable to cast object help!!
PostPosted: Wed Jan 13, 2010 7:13 am 
Newbie

Joined: Tue Jul 28, 2009 8:28 am
Posts: 18
If I step into flush() it fails at the line:

public class ApplicationTypeCode: CodeListItem
{

/// <summary>
/// Gets or sets the code value.
/// </summary>
/// <value>The code value.</value>
public virtual new string CodeValue
{
get { return base.CodeValue; } <------
set { base.CodeValue = value; }
}


Top
 Profile  
 
 Post subject: Re: Unable to cast object help!!
PostPosted: Wed Jan 13, 2010 8:26 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Can you define a second property CodeValue2 (+ a mpping)which points to the same field and use that instead of CodeValue in the many-to-one mapping. I suspect that CodeValue also being part of the identiy definition causes your problem.

_________________
--Wolfgang


Top
 Profile  
 
 Post subject: Re: Unable to cast object help!!
PostPosted: Wed Jan 13, 2010 9:50 am 
Newbie

Joined: Tue Jul 28, 2009 8:28 am
Posts: 18
Tried this and am still getting the same exception: Unable to cast object of type 'System.String' to type '........BusinessEntities.CodeListItem'.


Top
 Profile  
 
 Post subject: Re: Unable to cast object help!!
PostPosted: Wed Jan 13, 2010 9:59 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Sorry, I'm running out of ideas. You can try the google group and see if somebody there knows the answer:

http://groups.google.com/group/nhusers?hl=en

_________________
--Wolfgang


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

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.