-->
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.  [ 5 posts ] 
Author Message
 Post subject: TrueFalse/YesNo and Legacy MySQL Database
PostPosted: Tue Jun 27, 2006 5:42 am 
Newbie

Joined: Tue Jun 27, 2006 5:29 am
Posts: 3
Location: Perth, Western Australia
Hi Folks,

This is my first serious look at NHibernate and I have to say I'm quite impressed at the package's power and fleixibility, it's exactly what I'm looking for in ORM software.


A project I'm working on requires that we keep the same database structure, which for some reason uses MySQL ENUM('1','0') for True/False fields, instead of the de-facto standard of ENUM('Y','N').

Objects with boolean fields to match those columns come out of the database without throwing any exceptions, however the boolean fields are not being propogated. I've tried letting NHibernate determine the right type, and forcing it to YesNo and TrueFalse. Nothing works.

I've read the documentation and can see the query.substitution configuration option, however I've tried setting this (True 1, False 0, Yes 1, No 0), but this has not worked either.

I've searched these forums and come up with nothing that seems useful. I'm using straight XML mappings, and need this data to come through.

If anyone has any ideas on this, your help would be greatly appreciated.


NB: I'm using NHibernate 1.0.2 with C# and .NET 2.0.


Top
 Profile  
 
 Post subject: Re: TrueFalse/YesNo and Legacy MySQL Database
PostPosted: Tue Jun 27, 2006 6:18 am 
Newbie

Joined: Mon Jun 05, 2006 8:51 am
Posts: 18
The Evil One wrote:
If anyone has any ideas on this, your help would be greatly appreciated.

The most obvious choice would be to write custom type. Feed NHibernate.Nullables2 to Reflector to see the detail. I'd pay particular attention to implementation of NullableBooleanType.


Top
 Profile  
 
 Post subject: Re: TrueFalse/YesNo and Legacy MySQL Database
PostPosted: Tue Jun 27, 2006 6:36 am 
Newbie

Joined: Tue Jun 27, 2006 5:29 am
Posts: 3
Location: Perth, Western Australia
a.gogolev wrote:
The Evil One wrote:
If anyone has any ideas on this, your help would be greatly appreciated.

The most obvious choice would be to write custom type. Feed NHibernate.Nullables2 to Reflector to see the detail. I'd pay particular attention to implementation of NullableBooleanType.


I'd really like to keep the object model seperate from the data layer, which thus far I've managed to do.


Top
 Profile  
 
 Post subject: Re: TrueFalse/YesNo and Legacy MySQL Database
PostPosted: Tue Jun 27, 2006 6:56 am 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
The Evil One wrote:
I'd really like to keep the object model seperate from the data layer, which thus far I've managed to do.


Custom types allow you to retain the independence. Your domain model does not have to be aware of them.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jul 02, 2006 10:55 am 
Newbie

Joined: Tue Jun 27, 2006 5:29 am
Posts: 3
Location: Perth, Western Australia
Sorry, I didn't realise that the custom types are tied against the mappings, not the domain model. I managed to put together the following class which maps ENUM('1','0') values to System.Boolean values, which I've put here in case anyone wants it (note: it hasn't been thoroughly tested).


Code:
public class EnumBooleanType : IUserType
   {
      private static NullableType m_stringType = NHibernateUtil.AnsiString;
      private static string TrueValue = "1";
      private static string FalseValue = "0";


      public EnumBooleanType()
      {
      }

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

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

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

      public void NullSafeSet(System.Data.IDbCommand cmd, object value, int index)
      {
         string mySetValue = FalseValue;

         if( value.Equals(true) )
            mySetValue = TrueValue;

         m_stringType.Set(cmd, mySetValue, index);
      }

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

      public object NullSafeGet(System.Data.IDataReader rs, string[] names, object owner)
      {
         return m_stringType.NullSafeGet(rs, names).Equals(TrueValue);
      }

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


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