-->
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: MSSQL2005 xml DataType and NHibernate
PostPosted: Thu Nov 23, 2006 5:59 pm 
Newbie

Joined: Thu Nov 23, 2006 4:52 pm
Posts: 1
Good evening!

I'm using NHibernate 1.0.3 to interact with my MSSQL2005 database. There were no considerable problems till now. However at this moment I am forced to use tables containing columns of XML datatype. So I do not have any idea of creating persistent classes and mappings for these tables.

So, my questions are:
1. Does NHibernate supports MSSQL2005 XML datatype?
2. Which datatype should I use in class definitions? Could it be System.Xml.XmlNode? Will NHibernate accept this?
3. If there is no standard solution, then possibly someone already encountered such problem and solved it somehow?

Thanks in advance.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 27, 2006 11:07 am 
Newbie

Joined: Mon Jun 05, 2006 8:51 am
Posts: 18
As far as I know, NH does not support that particular datatype. It's only me guessing, but I think that MSSQL uses standard ntext column, so writing a user type to handle this will be quite simple:

Code:
public class XmlDocumentUserType : UserTypeBase
{
   public XmlDocumentUserType()
   {
   }

   #region UserTypeBase Members
   public override object NullSafeGet(IDataReader rs, string[] names, object owner)
   {
      object xmlDocument = rs[names[0]];
      if(xmlDocument == null || !(xmlDocument is string))
         return new XmlDocument();
      
      string xml = xmlDocument as string;
      XmlDocument effectiveXmlDocument = new XmlDocument();
      effectiveXmlDocument.LoadXml(xml);
      
      return effectiveXmlDocument;
   }

   public override void NullSafeSet(IDbCommand cmd, object value, int index)
   {
      XmlDocument xmlDocument = value as XmlDocument;
      (cmd.Parameters[index] as IDataParameter).Value = xmlDocument.InnerXml;
   }

   public override object DeepCopy(object value)
   {
      return (value as XmlDocument).Clone() as XmlDocument;
   }

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

   public override Type ReturnedType
   {
      get { return typeof(XmlDocument); }
   }
   #endregion
}

public abstract class UserTypeBase : IUserType
{
   public UserTypeBase()
   {
   }

   #region IUserTypeMembers
   public abstract object NullSafeGet(IDataReader rs, string[] names, object owner);

   public abstract void NullSafeSet(IDbCommand cmd, object value, int index);

   public abstract object DeepCopy(object value);

   public abstract SqlType[] SqlTypes
   { get; }

   public abstract Type ReturnedType
   { get; }

   public virtual bool IsMutable
   {
      get { return false; }
   }

   public virtual new bool Equals(object x, object y)
   {
      if(x != null)
         return x.Equals(y);

      return y == null;
   }
   #endregion
}
[/code]


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.