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: How to map a varchar on a boolean?
PostPosted: Thu Dec 21, 2006 8:52 pm 
Newbie

Joined: Tue Nov 14, 2006 7:35 pm
Posts: 6
Location: Mexico
Hello!
I have a field named PRD_STATUS of type VARCHAR2 on a table on one database. I cannot change this database but I must use it. This field only takes two different values: 'A' and 'B'.
On my system, the class that represents this table has a property named Status but it is of type Boolean, so I should map A to true and B to false. Is there any way I can do this?

My actual mapping looks like the following, although it obviously doesn't work:

<property name="Status" column="PRD_STATUS" type="Boolean" not-null="false" />

Note: I cannot change the class definition neither. Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 22, 2006 11:26 am 
Newbie

Joined: Thu Dec 21, 2006 5:48 pm
Posts: 4
There is a type in NHibernate that does something similar to this called NHibernate.Type.YesNoType that maps a boolean value to a Y/N in the database. You could use this class (from the source) as a template to build your own. It looks like you just have to override a few properties on the CharBooleanType to accomplish what you need. This code might even work (I just modified YesNoType).

Code:
   [Serializable]
   public class YourNewABBooleanType: CharBooleanType
   {
      /// <summary></summary>
      public YourNewABBooleanType() : base( new AnsiStringFixedLengthSqlType( 1 ) )
      {
      }

      /// <summary></summary>
      protected override sealed string TrueString
      {
         get { return "A"; }
      }

      /// <summary></summary>
      protected override sealed string FalseString
      {
         get { return "B"; }
      }

      /// <summary></summary>
      public override string Name
      {
         get { return "YourNewABBoolean"; }
      }
   }


Once you have your class built, just reference it from the mapping file.
Code:
    <property name="Status" type="YourNamespace.YourNewABBooleanType, YourAssembly">
      <column name="PRD_STATUS" length="1" sql-type="varchar" not-null="true" />
    </property>


Good luck,
-jason


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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.