-->
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.  [ 2 posts ] 
Author Message
 Post subject: Use of database field to type Char(x)
PostPosted: Wed Nov 16, 2005 5:37 pm 
Newbie

Joined: Wed Dec 22, 2004 12:04 pm
Posts: 7
How can i use a char(2) in the database to map the content into a string object


Code:
public string Code
{
   get
   {
   return code;
   }
   set
   {
   code=value;
   }
}


Code:
<class name="CVSM.NMP.BusinessEntity.impl.Conformation, CVSM.NMP.BusinessEntity" table="w09con">
  ...
<property name="Code" type="String" column="CODE_ID" length="2" />
   
...
</class>


I use oracle 9 and my columns is define as CHAR(2)

I try to use the type char in the hbm config file but i have a class cast exception.


Top
 Profile  
 
 Post subject: Multiple char solution
PostPosted: Fri Nov 18, 2005 11:49 am 
Newbie

Joined: Wed Dec 22, 2004 12:04 pm
Posts: 7
I have implement a IUserType to provide solution for mapping for char with length more than one

If you find an improvement or Bug in this code. Please send me and email

Code:
/// <summary>
   /// Summary description for MultipleCharType.
   /// </summary>
   public class MultipleCharType:NHibernate.IUserType
   {
      private static readonly bool IsDebugEnabled;
      private static NullableType multipleChar = NHibernate.NHibernateUtil.String;

      static MultipleCharType()
      {
         //cache this, because it was a significant performance cost
         IsDebugEnabled = LogManager.GetLogger( typeof( IType ).Namespace ).IsDebugEnabled;
      }

      private ILog Log
      {
         get { return LogManager.GetLogger( GetType() ); }
      }

      public MultipleCharType()
      {
      }

      #region IUserType Members

      public new bool Equals(object x, object y)
      {
         if(x==y) return true;

         string lhs = (x==null) ? null : (string)x;
         string rhs = (y==null) ? null : (string)y;

         return multipleChar.Equals(lhs, rhs);
      }

      public NHibernate.SqlTypes.SqlType[] SqlTypes
      {
         get
         {
            return new SqlType[] { multipleChar.SqlType };
         }
      }

      public object DeepCopy(object value)
      {
         return value;
      }

      public void NullSafeSet(System.Data.IDbCommand cmd, object value, int index)
      {
         if(value.Equals(null))
         {
            if( IsDebugEnabled )
            {
               Log.Debug( "binding null to parameter: " + index.ToString() );
            }
            ( (IDbDataParameter)cmd.Parameters[index]).Value = DBNull.Value;
         }
         else
         {
            if( IsDebugEnabled )
            {
               Log.Debug( "binding '" + value.ToString() + "' to parameter: " + index );
            }
            //Change the DBtype because the find query will not work
            IDataParameter parm = cmd.Parameters[ index ] as IDataParameter;
            parm.DbType=System.Data.DbType.AnsiStringFixedLength;
            parm.Value = value;
         }
      }

      public Type ReturnedType
      {
         get { return typeof(System.String); }
      }

      public object NullSafeGet(System.Data.IDataReader rs, string[] names, object owner)
      {
         int index = rs.GetOrdinal( names[0] );

         if( rs.IsDBNull( index ) )
         {

            if( IsDebugEnabled )
            {
               Log.Debug( "returning null as column: " + names[0]  );
            }
            return null;
         }
         else
         {
            string  charValue  =(string)multipleChar.NullSafeGet(rs, names);
            if( IsDebugEnabled )
            {
               Log.Debug( "returning '" +  charValue  + "' as column: " + names[0] );
            }
            return charValue.TrimEnd(' ');
         }
      }

      public bool IsMutable
      {
            get { return multipleChar.IsMutable; }
      }

      #endregion
   }


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.