-->
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.  [ 4 posts ] 
Author Message
 Post subject: Mapping String Value to SqlServer 2005 XML Column
PostPosted: Tue Jul 24, 2007 11:14 am 
Beginner
Beginner

Joined: Mon Nov 07, 2005 11:06 pm
Posts: 28
Has anyone been able to map a string value of an object to the new xml data / column type in SqlServer 2005? The new column type offers a lot of nice features for working with XML. The way I currently map xml to the DB is by using an ntext column and a StringClob type in the mapping file. Unfortunatley, the ntext column does not offer the advanced features of working with the xml.

I appreciate any ideas of suggestions.

Thanks,

Kevin


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 24, 2007 6:17 pm 
Beginner
Beginner

Joined: Wed Oct 04, 2006 8:57 am
Posts: 25
You could use this class:

Code:
    public class XmlType : IUserType
    {
        public new bool Equals(object x, object y)
        {
            XmlDocument xdoc_x = (XmlDocument)x;
            XmlDocument xdoc_y = (XmlDocument)y;
            return xdoc_y.OuterXml == xdoc_x.OuterXml;
        }

        public object NullSafeGet(IDataReader rs, string[] names, object owner)
        {
            if (names.Length != 1)
                throw new InvalidOperationException("names array has more than one element. can't handle this!");
            XmlDocument document = new XmlDocument();
            string val = rs[names[0]] as string;
            if (val != null)
            {
                document.LoadXml(val);
                return document;
            }
            return null;
        }

        public void NullSafeSet(IDbCommand cmd, object value, int index)
        {
            DbParameter parameter = (DbParameter)cmd.Parameters[index];
            if (value == null)
            {
                parameter.Value = DBNull.Value;
                return;
            }
            parameter.Value = ((XmlDocument)value).OuterXml;
        }

        public object DeepCopy(object value)
        {
            XmlDocument other = (XmlDocument)value;
            XmlDocument xdoc = new XmlDocument();
            xdoc.LoadXml(other.OuterXml);
            return xdoc;
        }

        public SqlType[] SqlTypes
        {
            get
            {
                return new SqlType[] { new SqlXmlType() };
            }
        }

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

        public bool IsMutable
        {
            get { return true; }
        }

        #region IUserType Members

        public object Assemble(object cached, object owner)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        public object Disassemble(object value)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        public int GetHashCode(object x)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        public object Replace(object original, object target, object owner)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        #endregion
    }

    public class SqlXmlType : SqlType
    {
        public SqlXmlType()
            : base(System.Data.DbType.Xml)
        {
        }
    }


and then map it:

Code:
<property name="MyXmlDocument" column="myXmlColumn" type="XmlType"/> 


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 24, 2007 9:47 pm 
Beginner
Beginner

Joined: Mon Nov 07, 2005 11:06 pm
Posts: 28
Thanks for the information. I am not familiar with user types in NHibernate. Can you provide a brief explanation of the strategy being used in this example? What parts of this class are specific to implementing a user type and what parts are specific to the xml sql data type? I appreciate any additional help you can give!

Thanks,

Kevin


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 25, 2007 4:09 am 
Beginner
Beginner

Joined: Wed Oct 04, 2006 8:57 am
Posts: 25
Every method on that class is a requirement of IUserType. Some of the ones that aren't implemented are because I don't know what they do, but I have been able to use this class regardless (if someone cares to enlighten me please do!)

The idea of the UserType is that it tells nhibernate how to map a class (in this case an XmlDocument) to the database.

I think the key part is NullSafeGet and NullSafeSet in these methods we are getting/setting the value directly from/to the Paramater attached to the IDbCommand (part of ado.net).


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