>       at NHibernate.Type.ByteType.Set(IDbCommand cmd, Object value,

Ah, so the problem involves a Byte.

Looking through the mapping of MRImageBase I find:

>    <property name="Format"
>              column="FORMAT"
>              not-null="true"
>              type="Byte"
>    />


But in the class Format is a string!?!
>        public virtual string Format
>        {
>            set { format = value; }
>            get { return format; }
>        }


(Maybe there are more Bytes, I didn't look any further.)

Note that in many cases you don't need to specify the type attribute
in the mappings. NHibernate can figure it out through reflection.


/Oskar


2010/1/28 Joksim <[email protected]>:
> Hi,
>
> I'm new to nHiberante and I have a problem. I have a database that is
> maped to several hbm.xml files. Some of the tables use inheritance
> (1:1 relations), store pictures, etc. When I call
>
> MRImage x = MRI_CBIR_BL.FileManagement.FileHelper.CreateImageDAO
> ("ffff");
> ssn.SaveOrUpdate(x);
>
> I get an error:
>
> System.InvalidCastException was unhandled by user code
>  Message="Specified cast is not valid."
>  Source="NHibernate"
>  StackTrace:
>       at NHibernate.Type.ByteType.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, Boolean[] settable, ISessionImplementor
> session)
>       at NHibernate.Persister.Entity.AbstractEntityPersister.Dehydrate
> (Object id, Object[] fields, Object rowId, Boolean[] includeProperty,
> Boolean[][] includeColumns, Int32 table, IDbCommand statement,
> ISessionImplementor session, Int32 index)
>       at NHibernate.Persister.Entity.AbstractEntityPersister.Dehydrate
> (Object id, Object[] fields, Boolean[] includeProperty, Boolean[][]
> includeColumns, Int32 j, IDbCommand st, ISessionImplementor session)
>       at
> NHibernate.Persister.Entity.AbstractEntityPersister.GeneratedIdentifierBinder.BindValues
> (IDbCommand ps)
>       at NHibernate.Id.Insert.AbstractReturningDelegate.PerformInsert
> (SqlCommandInfo insertSQL, ISessionImplementor session, IBinder
> binder)
>       at NHibernate.Persister.Entity.AbstractEntityPersister.Insert
> (Object[] fields, Boolean[] notNull, SqlCommandInfo sql, Object obj,
> ISessionImplementor session)
>       at NHibernate.Persister.Entity.AbstractEntityPersister.Insert
> (Object[] fields, Object obj, ISessionImplementor session)
>       at NHibernate.Action.EntityIdentityInsertAction.Execute()
>       at NHibernate.Engine.ActionQueue.Execute(IExecutable
> executable)
>       at
> NHibernate.Event.Default.AbstractSaveEventListener.PerformSaveOrReplicate
> (Object entity, EntityKey key, IEntityPersister persister, Boolean
> useIdentityColumn, Object anything, IEventSource source, Boolean
> requiresImmediateIdAccess)
>       at
> NHibernate.Event.Default.AbstractSaveEventListener.PerformSave(Object
> entity, Object id, IEntityPersister persister, Boolean
> useIdentityColumn, Object anything, IEventSource source, Boolean
> requiresImmediateIdAccess)
>       at
> NHibernate.Event.Default.AbstractSaveEventListener.SaveWithGeneratedId
> (Object entity, String entityName, Object anything, IEventSource
> source, Boolean requiresImmediateIdAccess)
>       at
> NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.SaveWithGeneratedOrRequestedId
> (SaveOrUpdateEvent event)
>       at
> NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.EntityIsTransient
> (SaveOrUpdateEvent event)
>       at
> NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.PerformSaveOrUpdate
> (SaveOrUpdateEvent event)
>       at
> NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.OnSaveOrUpdate
> (SaveOrUpdateEvent event)
>       at NHibernate.Impl.SessionImpl.FireSaveOrUpdate
> (SaveOrUpdateEvent event)
>       at NHibernate.Impl.SessionImpl.SaveOrUpdate(Object obj)
>       at MRI_CBIR._Default.Page_Load(Object sender, EventArgs e) in F:
> \My Dropbox\MRI\Project\mri_cbir\MRI_CBIR\Default.aspx.cs:line 33
>       at System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr
> fp, Object o, Object t, EventArgs e)
>       at System.Web.Util.CalliEventHandlerDelegateProxy.Callback
> (Object sender, EventArgs e)
>       at System.Web.UI.Control.OnLoad(EventArgs e)
>       at System.Web.UI.Control.LoadRecursive()
>       at System.Web.UI.Page.ProcessRequestMain(Boolean
> includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
>  InnerException:
>
> -------------------------
>
> I use native generator with Sql Server 2005.
>
> The classes are: nd mappings are:
>
> ---------------base class-----------------------------
>
> using System;
> using System.Collections.Generic;
> using System.Text;
>
> using System.Drawing;
> using NHibernate;
> using System.Collections;
>
> namespace MRI_CBIR_DAL.ClassMappings
> {
>    public class MRImageBase
>    {
>        private long id;
>
>        private string name;
>
>        private String format;
>
>        private string pathToImage;
>
>        private string pathToThumbnail;
>
>        private Bitmap blob;
>
>        private Image thumbnail;
>
>
>        //// Foreign Keys
>
>        /// <summary>
>        /// The set where this image belongs
>        /// </summary>
>        private MRISet imageSet;
>
>        /// <summary>
>        /// The class of the image
>        /// </summary>
>        private MRIClass imageClass;
>
>
>        //// Records referencing to the current object
>
>        /// <summary>
>        /// Dictionary of all descriptors (for NHibernate mappings)
>        /// </summary>
>        private IDictionary descriptors;
>
>
>        // Constuctors
>        public MRImageBase() { }
>
>
>        // Properties
>
>        public virtual long Id
>        {
>            set { id = value; }
>            get { return id; }
>        }
>
>
>        public virtual string Name
>        {
>            set { name = value; }
>            get { return name; }
>        }
>
>        public virtual string Format
>        {
>            set { format = value; }
>            get { return format; }
>        }
>
>        public virtual string PathToImage
>        {
>            set { pathToImage = value; }
>            get { return pathToImage; }
>        }
>
>        public virtual string PathToThumbnail
>        {
>            set { pathToThumbnail = value; }
>            get { return pathToThumbnail; }
>        }
>
>        public virtual Bitmap Blob
>        {
>            set { blob = value; }
>            get { return blob; }
>        }
>
>        public virtual Image Thumbnail
>        {
>            set { thumbnail = value; }
>            get { return thumbnail; }
>        }
>
>
>        //// Foreign Keys
>
>        public virtual MRISet ImageSet
>        {
>            get { return imageSet; }
>            set { imageSet = value; }
>        }
>
>        public virtual MRIClass ImageClass
>        {
>            get { return imageClass; }
>            set { imageClass = value; }
>        }
>
>
>        //// Records referencing to the current object
>        public virtual IDictionary Descriptors
>        {
>            set { this.descriptors = value; }
>            get { return this.descriptors; }
>        }
>
>    }
> }
>
> ---------------inherited class-----------------------------
>
> using System;
> using System.Collections.Generic;
> using System.Text;
>
> using NHibernate;
> using System.Collections;
>
>
> namespace MRI_CBIR_DAL.ClassMappings
> {
>    /// <summary>
>    /// Class used to represent records from the table IMAGE via
> NHibernate
>    /// </summary>
>    public class MRImage : MRImageBase
>    {
>
>        private String imageCode;
>
>
>        private string description;
>
>        private string diagnosis;
>
>        private Int16 width;
>
>        private Int16 height;
>
>        private Int16 resolutionX;
>
>        private Int16 resolutionY;
>
>        private IDictionary segments;
>
>        public MRImage()
>        { }
>
>        public virtual string ImageCode
>        {
>            set { imageCode = value; }
>            get { return imageCode; }
>        }
>
>        public virtual string Description
>        {
>
>            set { description = value; }
>            get { return description; }
>
>        }
>
>        public virtual string Diagnosis
>        {
>
>            set { diagnosis = value; }
>            get { return diagnosis; }
>
>        }
>
>        public virtual Int16 Width
>        {
>            get { return this.width; }
>            set { this.width = value; }
>        }
>
>        public virtual Int16 Height
>        {
>            get { return this.height; }
>            set { this.height = value; }
>        }
>
>        public virtual Int16 ResolutionX
>        {
>            get { return this.resolutionX; }
>            set { this.resolutionX = value; }
>        }
>
>        public virtual Int16 ResolutionY
>        {
>            get { return this.resolutionY; }
>            set { this.resolutionY = value; }
>        }
>
>        public virtual IDictionary Segments
>        {
>            get { return this.segments; }
>            set { this.segments = value; }
>        }
>
>        public override long Id
>        {
>            get
>            {
>                return base.Id;
>            }
>            set
>            {
>                base.Id = value;
>            }
>        }
>
>        public override string Name
>        {
>            get
>            {
>                return base.Name;
>            }
>            set
>            {
>                base.Name = value;
>            }
>        }
>
>        public override string Format
>        {
>            get
>            {
>                return base.Format;
>            }
>            set
>            {
>                base.Format = value;
>            }
>        }
>
>        public override string PathToImage
>        {
>            get
>            {
>                return base.PathToImage;
>            }
>            set
>            {
>                base.PathToImage = value;
>            }
>        }
>
>        public override string PathToThumbnail
>        {
>            get
>            {
>                return base.PathToThumbnail;
>            }
>            set
>            {
>                base.PathToThumbnail = value;
>            }
>        }
>
>        public override System.Drawing.Bitmap Blob
>        {
>            get
>            {
>                return base.Blob;
>            }
>            set
>            {
>                base.Blob = value;
>            }
>        }
>
>        public override System.Drawing.Image Thumbnail
>        {
>            get
>            {
>                return base.Thumbnail;
>            }
>            set
>            {
>                base.Thumbnail = value;
>            }
>        }
>
>        public override IDictionary Descriptors
>        {
>            get
>            {
>                return base.Descriptors;
>            }
>            set
>            {
>                base.Descriptors = value;
>            }
>        }
>
>        public override MRIClass ImageClass
>        {
>            get
>            {
>                return base.ImageClass;
>            }
>            set
>            {
>                base.ImageClass = value;
>            }
>        }
>
>        public override MRISet ImageSet
>        {
>            get
>            {
>                return base.ImageSet;
>            }
>            set
>            {
>                base.ImageSet = value;
>            }
>        }
>
>    }
> }
>
>
> ---------------class mappings-----------------------------
>
> <?xml version="1.0" encoding="utf-8" ?>
>
> <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
>    namespace="MRI_CBIR_DAL.ClassMappings" assembly="MRI_CBIR_DAL">
>
>  <class name="MRImageBase" table="IMAGE_BASE">
>
>    <id name="Id" type="Int32" column="ID">
>      <generator class="native" />
>    </id>
>
>    <property name="Name"
>              column="NAME"
>              not-null="true"
>              type="String(75)"
>    />
>
>    <property name="Format"
>              column="FORMAT"
>              not-null="true"
>              type="Byte"
>    />
>
>    <property name="PathToImage"
>              column="PATH_TO_IMAGE"
>              not-null="false"
>              type="StringClob"
>    />
>
>    <property name="PathToThumbnail"
>              column="PATH_TO_THUMB"
>              not-null="false"
>              type="StringClob"
>    />
>
>    <property name="Blob"
>              column="IMG"
>              not-null="false"
>              type="BinaryBlob"
>    />
>
>    <property name="Thumbnail"
>              column="THUMB"
>              not-null="false"
>              type="BinaryBlob"
>    />
>
>    <set name="Descriptors"
>         cascade="all">
>
>      <key column="IMAGE_ID" />
>      <one-to-many class="MRI_CBIR_DAL.ClassMappings.Descriptor,
> MRI_CBIR_DAL" />
>
>    </set>
>
>    <many-to-one name="ImageClass"
>                 class="MRI_CBIR_DAL.ClassMappings.MRIClass,
> MRI_CBIR_DAL"
>                 column="CLASS_ID"
>    />
>
>    <many-to-one name="ImageSet"
>                 class="MRI_CBIR_DAL.ClassMappings.MRISet,
> MRI_CBIR_DAL"
>                 column="SET_ID"
>    />
>
>    <joined-subclass name="MRImage" table="IMAGE">
>
>      <key column="ID"/>
>
>      <property name="ImageCode"
>                column="CODE"
>                not-null="false"
>                type="String(50)"
>        />
>
>      <property name="Description"
>                column="DESCRIPTION"
>                not-null="false"
>                type="String"
>        />
>
>      <property name="Diagnosis"
>                column="DIAGNOSIS"
>                not-null="false"
>                type="String"
>        />
>
>      <property name="Width"
>                column="WIDTH"
>                not-null="false"
>                type="Int16"
>        />
>
>      <property name="Height"
>                column="HEIGHT"
>                not-null="false"
>                type="Int16"
>        />
>
>      <property name="ResolutionX"
>          column="RES_X"
>          not-null="false"
>          type="Int16"
>        />
>
>      <property name="ResolutionY"
>                column="RES_Y"
>                not-null="false"
>                type="Int16"
>        />
>      <set name="Segments"
>           cascade="all">
>
>        <key column="IMAGE_ID" />
>        <one-to-many class="MRI_CBIR_DAL.ClassMappings.Segment,
> MRI_CBIR_DAL" />
>
>      </set>
>
>    </joined-subclass >
>
>    <joined-subclass name="Segment" table="SEGMENT">
>      <key column="Id"/>
>
>      <property name="RoI">
>        <column name="ROI" not-null="true" />
>      </property>
>
>      <property name="Technique">
>        <column name="Technique" not-null="true"/>
>      </property>
>
>      <property name="Description">
>        <column name="Description" not-null="false"/>
>      </property>
>
>      <property name="Pathology">
>        <column name="Pathology" not-null="false"/>
>      </property>
>
>      <many-to-one name="ImageMapper"
>                   class="MRI_CBIR_DAL.ClassMappings.MRImage,
> MRI_CBIR_DAL"
>                   column="IMAGE_ID"
>      />
>    </joined-subclass>
>
>  </class>
>
> </hibernate-mapping>
>
>
>
> Sorry for the long codes and message
>
> Thanks,
> Joksim
>
> --
> You received this message because you are subscribed to the Google Groups 
> "nhusers" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to 
> [email protected].
> For more options, visit this group at 
> http://groups.google.com/group/nhusers?hl=en.
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"nhusers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/nhusers?hl=en.

Reply via email to