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: Custom mapping for bool type
PostPosted: Mon Aug 10, 2009 4:12 pm 
Newbie

Joined: Tue Jun 30, 2009 10:13 am
Posts: 6
Good afternoon,

We have a set of tables where we store boolean values as a nvarchar(1) where '1' = true and '0' = false.

I am trying to implement a custom type for these columns using the following:

Code:
using NHibernate.Type;
using System;

namespace com.purefacts.db.hibernate.type
{
    /// <summary>
    /// NHibernate transform for the Boolean type.
    /// </summary>
    ///
    /// <remarks>
    /// We stored in a nvarchar(1) field the values of '1' for TRUE and '0' for FALSE
    /// </remarks>
    ///
    public class BooleanType : EnumStringType
    {
        public BooleanType() : base(typeof(BooleanEnum)) {
        }

        public override object GetValue(object code) {

            if (null == code) {
                return "0";
            }

            switch ((bool)code) {
                case true:
                    return "1";
                case false:
                    return "0";
                default:
                    throw new ArgumentException("Invalid BooleanEnum.");
            }
        }

        public override object GetInstance(object code) {

            if (code == null) {
                return false;
            }

            else {
                String strCode = ((String)code).ToUpper();
                switch (strCode) {
                    case " ":
                        return false;
                        break;
                    case "1":
                        return true;
                        break;
                    case "0":
                        return false;
                        break;
                    default:
                        throw new ArgumentException("Cannot convert code '" + code + "' to BooleanEnum.");
                }
            }
        }
    }

    /// <summary>The BOOLEAN enumeration.</summary>
    public enum BooleanEnum
    {
        TRUE,
        FALSE
    }
}



and my HBM entry is

Code:
<class...
...
<property name="Active" column="Active" type="com.purefacts.db.hibernate.type.BooleanType, SpringAir.Core" access="property"/>
...
</class>


which seems to be working but looks a little strange, is this the correct way to accomplish this?

Furthermore, why can't I provide my own type values to the TrueFalse type so I can store '1' or '0' instead of 'T' and 'F'

Thanks in advance.


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.