NHibernate and XML Column Type

本文介绍了一个NHibernate的扩展,该扩展允许将XML数据保存到数据库中。通过此扩展,可以将XMLDocument对象直接存储在数据库表中,并展示了如何在业务实体类及映射文件中配置该类型。
 

这是Ayende Rahien对NHibernate所做的一个扩展,让NHibernate可以保存XML类型的数据到数据库中,这在某些情况下非常有用。可以把XML转换为XmlDocument保存,也可以直接把对象序列化成XML保存到数据库中。看一下简单的使用过程:

业务实体类

None.gifpublic class Document
ExpandedBlockStart.gifContractedBlock.gifdot.gif{
InBlock.gif    int id;
InBlock.gif
InBlock.gif    string author;
InBlock.gif
InBlock.gif    XmlDocument xml;
InBlock.gif
InBlock.gif
InBlock.gif    public Document(string author, XmlDocument xml)
ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
InBlock.gif        this.author = author;
InBlock.gif
InBlock.gif        this.xml = xml;
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    public Document()
ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
InBlock.gif
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    public int Id
ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        get dot.gif{ return id; }
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        set dot.gif{ id = value; }
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    public string Author
ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        get dot.gif{ return author; }
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        set dot.gif{ author = value; }
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    public XmlDocument Xml
ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        get dot.gif{ return xml; }
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        set dot.gif{ xml = value; }
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

映射文件,注意xml属性的类型,下载的代码中有XmlType的实现。

None.gif<?xml version='1.0' encoding='utf-8'?>
None.gif
None.gif<hibernate-mapping
None.gif
None.gif     xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
None.gif
None.gif     xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns='urn:nhibernate-mapping-2.0'>
None.gif
None.gif  <class name='NHibernate.Xml.Document,eXmlDataType2003' table='Documents'>
None.gif
None.gif    <id name='Id'>
None.gif
None.gif      <generator class='identity'/>
None.gif
None.gif    </id>
None.gif
None.gif    <property name='Author'/>
None.gif
None.gif    <property name='Xml' column='xml' type='NHibernate.Xml.XmlType, XmlDataType2003' />
None.gif
None.gif  </class>
None.gif
None.gif</hibernate-mapping>

客户程序使用

 

None.gifpublic class Program
ExpandedBlockStart.gifContractedBlock.gifdot.gif{
InBlock.gif    static void Main(string[] args)
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
InBlock.gif        Configuration cfg = new Configuration();
InBlock.gif
InBlock.gif        cfg.AddAssembly(typeof (Document).Assembly);
InBlock.gif
InBlock.gif        ISessionFactory factory = cfg.BuildSessionFactory();
InBlock.gif
InBlock.gif        using(ISession session = factory.OpenSession())
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
InBlock.gif            XmlDocument xml = new XmlDocument();
InBlock.gif
InBlock.gif            xml.Load("c:\\temp\\Municipalities.xml");
InBlock.gif
InBlock.gif            Document doc1 = new Document("Author1",xml);
InBlock.gif
InBlock.gif            session.Save(doc1);
InBlock.gif
InBlock.gif            session.Flush();
InBlock.gif
InBlock.gif
InBlock.gif            Document document = session.Load(typeof (Document),1) as Document;
InBlock.gif
InBlock.gif           Console.WriteLine(document.Xml.OuterXml);
InBlock.gif
InBlock.gif            Console.ReadLine();
InBlock.gif
InBlock.gif           session.Flush();
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}
数据库中的结果

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值