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.  [ 6 posts ] 
Author Message
 Post subject: Map char(1) column with only 'Y' or 'N' to boolean type?
PostPosted: Wed Oct 31, 2007 1:54 pm 
Newbie

Joined: Wed Oct 31, 2007 1:50 pm
Posts: 13
What is the best route for mapping an oracle char(1) column that has either Y or N to designate Yes or No to a boolean property? Is an IInterceptor the best route or a custom IUserType/ICompositeUserType?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 31, 2007 4:47 pm 
Newbie

Joined: Fri May 11, 2007 3:53 pm
Posts: 12
You want to use the built-in Hibernate type yes_no.

This is directly from the documentation!
================================
5.2.2. Basic value types

The built-in basic mapping types may be roughly categorized into

integer, long, short, float, double, character, byte, boolean, yes_no, true_false

Type mappings from Java primitives or wrapper classes to appropriate (vendor-specific) SQL column types. boolean, yes_no and true_false are all alternative encodings for a Java boolean or java.lang.Boolean.
================================

So in your mapping file:

<property name="myBoolean" type="yes_no">
<column name="myCharColumn"/>
</property>

So if you set the boolean to true a 'Y' will be put in the column and for false a 'N'.


- Don't forget to rate!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 31, 2007 7:42 pm 
Newbie

Joined: Wed Oct 31, 2007 1:50 pm
Posts: 13
You know, I ended up finding it, but only by Reflectoring the NHibernate Types namespace. It just never showed up in my searches - especially not that specific portion.

Thanks for the help - it's nice to see these forums as active.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 01, 2007 1:29 pm 
Newbie

Joined: Fri May 11, 2007 3:53 pm
Posts: 12
Great! Even though you might have found it. I did spend time to post and help you out. So please don't forget to rate!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 01, 2007 1:57 pm 
Newbie

Joined: Wed Oct 31, 2007 1:50 pm
Posts: 13
Done.

They need to make the rating buttons/links bigger - more obvious. I hadn't even realized they were there.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 02, 2007 2:26 pm 
Expert
Expert

Joined: Fri Oct 28, 2005 5:38 pm
Posts: 390
Location: Cedarburg, WI
Here is a useful custom user type we use to deal with nullable char(1) columns containing Y/N:
Code:
using System;
using System.Data;

using NH  = NHibernate;
using NNH = Nullables.NHibernate;

namespace OurCompany.DataAccess.Providers.NHibernate.UserTypes
{
   /// <summary>
   /// A NHibernate <see cref="NH::Type.IType"/> for a <see cref="System.Nullable&lt;Boolean&gt;"/>
   /// mapped to a CHAR(1) database column with values 'Y', 'N' or NULL.
   /// </summary>
   public class NullableYesNoType : NNH.NullableTypesType
   {
      public NullableYesNoType() : base(new NH.SqlTypes.StringFixedLengthSqlType(1))
      {
      }

      public override object NullValue
      {
         get {return null;}
      }

      public override System.Type ReturnedClass
      {
         get {return typeof(bool?);}
      }

      public override object Get(IDataReader rs, int index)
      {
         object value = rs[index];

         if (value == DBNull.Value)
         {
            return null;
         }
         else
         {
            return (Convert.ToChar(value) == 'Y');
         }
      }

      public override void Set(IDbCommand cmd, object value, int index)
      {
         IDataParameter parameter = (IDataParameter) cmd.Parameters[index];
         bool? nullableValue = (bool?) value;

         if (nullableValue.HasValue)
         {
            parameter.Value = (nullableValue.Value ? 'Y' : 'N');
         }
         else
         {
            parameter.Value = DBNull.Value;
         }
      }

      public override object FromStringValue(string xml)
      {
         if (xml == null || xml.TrimEnd().Length == 0)
         {
            return null;
         }
         else
         {
            return (xml[0] == 'Y');
         }
      }
   }
}


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