-->
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: Defining custom types (using ICompositeUserType)
PostPosted: Thu Feb 09, 2006 6:41 pm 
Newbie

Joined: Thu Aug 11, 2005 5:58 pm
Posts: 6
Hi!

I would like to create a custom NHibernate type called Rectangle reflecting 4 int columns in a table. I read DoubleStringType.cs and followed it's implementation. According to Hibernate's cut example, I have two files called Rectangle.cs and RectangleType.cs. Is this correct for NHibernate as well? I'm currently using a types.hbm.xml file for connecting the Rectangle class and the persister class RectangleType though this is not mentioned in the NHibernate docs.

Could you tell me how to get the custom type working?

The files are:


Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
  <typedef name="Rectangle" class="MyApp.Persistence.RectangleType, MyApp.Persistence"/>
</hibernate-mapping>


My implementation looks as follows (RectangleType.cs):

Code:
using System;
using System.Collections.Generic;
using System.Text;
using NHibernate;

namespace MyApp
{
    namespace Persistence
    {
        public class RectangleType : NHibernate.ICompositeUserType
        {
            public System.Type ReturnedClass
            {
                get { return typeof(int[]); }
            }


            public bool IsMutable
            {
                get { return true; }
            }


            public String[] PropertyNames
            {
                get
                {
                    return new String[] { "x", "y", "w", "h" };
                }
            }


            public NHibernate.Type.IType[] PropertyTypes
            {
                get
                {
                    return new NHibernate.Type.IType[] { NHibernateUtil.Int32, NHibernateUtil.Int32,
                                                         NHibernateUtil.Int32, NHibernateUtil.Int32 };
                }
            }


            public object Assemble(object cached, NHibernate.Engine.ISessionImplementor session, object owner)
            {
                return DeepCopy(cached);
            }


            public object Disassemble(Object value, NHibernate.Engine.ISessionImplementor session)
            {
                return DeepCopy(value);
            }


            public new bool Equals(object a, object b)
            {
                if (a==b) return true;
                if (a==null || b==null) return false;
               
                int[] lhs = (int[])a;
                int[] rhs = (int[])b;

                return lhs[0].Equals(rhs[0]) && lhs[1].Equals(rhs[1]) && lhs[2].Equals(rhs[2]) && lhs[3].Equals(rhs[3]);             
            }


            public Object DeepCopy(Object a)
            {
                if (a==null) return null;
                int[] result = new int[4];
                int[] input = (int[])a;

                result[0] = input[0];
                result[1] = input[1];
                result[2] = input[2];
                result[3] = input[3];

                return result;
            }


            public Object NullSafeGet(System.Data.IDataReader rs, String[] names, NHibernate.Engine.ISessionImplementor session, Object owner)
            {               
                int x = (int)NHibernateUtil.Int32.NullSafeGet(rs, names[0], session, owner);
                int y = (int)NHibernateUtil.Int32.NullSafeGet(rs, names[1], session, owner);
                int w = (int)NHibernateUtil.Int32.NullSafeGet(rs, names[2], session, owner);
                int h = (int)NHibernateUtil.Int32.NullSafeGet(rs, names[3], session, owner);

                return (x==null && y==null && w==null && h==null) ? null : new Rectangle(x, y, w, h);
            }


            public void NullSafeSet(System.Data.IDbCommand st, Object value, int index, NHibernate.Engine.ISessionImplementor session)
            {
                Rectangle rect = (value==null) ? new Rectangle() : (Rectangle)value;               

                NHibernateUtil.Int32.NullSafeSet(st, rect.x, index, session);
                NHibernateUtil.Int32.NullSafeSet(st, rect.y, index+1, session);
                NHibernateUtil.Int32.NullSafeSet(st, rect.w, index+2, session);
                NHibernateUtil.Int32.NullSafeSet(st, rect.h, index+3, session);
            }


            public Object GetPropertyValue(Object component, int property)
            {
                Rectangle rect = (Rectangle)component;

                switch (property)
                {
                    case 0:
                        {
                            return rect.x;
                            break;
                        }
                    case 1:
                        {
                            return rect.y;
                            break;
                        }
                    case 2:
                        {
                            return rect.w;
                            break;
                        }
                    case 3:
                        {
                            return rect.h;
                            break;
                        }
                }

                return null;
            }


            public void SetPropertyValue(Object component, int property, Object value)
            {
                Rectangle rect = (Rectangle)component;

                switch (property)
                {
                    case 0:
                        {
                            rect.x = System.Convert.ToInt32(value);
                            break;
                        }
                    case 1:
                        {
                            rect.y = System.Convert.ToInt32(value);
                            break;
                        }
                    case 2:
                        {
                            rect.w = System.Convert.ToInt32(value);
                            break;
                        }
                    case 3:
                        {
                            rect.h = System.Convert.ToInt32(value);
                            break;
                        }               
                }
            }
        }
    }
}


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 10, 2006 5:19 am 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
There is no typedef in NHibernate, it's a H3 feature that has not been ported. Use <property name="MyRectangleProperty" type="MyApp.Persistence.RectangleType, MyApp.Persistence" />.


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.