asp.net MVC2 初探六

本文介绍如何将Nhibernate与Spring框架进行集成,并详细解释了配置过程及关键配置项。针对使用复合主键时可能出现的问题,提供了具体的解决方案。
本节大致讲解Nhibernate和Spring的结合及其配置
<?xml version="1.0" encoding="utf-8" ?> 
<objects xmlns="http://www.springframework.net" 
                 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
                 xsi:schemaLocation="http://www.springframework.net http://www.springframework.net/xsd/spring-objects.xsd"> 

    <object id="SessionFactory" type="Spring.Data.NHibernate.LocalSessionFactoryObject, Spring.Data.NHibernate12"> 
        <property name="DbProvider" ref="DbProvider" /> 
        <property name="MappingAssemblies"> 
            <list> 
                <value>Model</value> 
            </list> 
        </property> 
        <property name="HibernateProperties"> 
            <dictionary> 
                <entry key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider" /> 
                <entry key="hibernate.dialect" value="NHibernate.Dialect.MsSql2005Dialect" /> 
                <entry key="hibernate.connection.driver_class" value="NHibernate.Driver.SqlClientDriver" /> 
                <entry key="show_sql" value="true" /> 
            </dictionary> 
        </property> 
    </object> 

    <object id="HibernateTransactionManager" type="Spring.Data.NHibernate.HibernateTransactionManager, Spring.Data.NHibernate12"> 
        <property name="DbProvider" ref="DbProvider" /> 
        <property name="sessionFactory" ref="sessionFactory" /> 
    </object> 

    <object id="TransactionInterceptor" type="Spring.Transaction.Interceptor.TransactionInterceptor, Spring.Data"> 
        <property name="TransactionManager" ref="HibernateTransactionManager" /> 
        <property name="TransactionAttributeSource"> 
            <object type="Spring.Transaction.Interceptor.AttributesTransactionAttributeSource, Spring.Data" /> 
        </property> 
    </object> 

    <object id="DbProvider" type="DAO.SQLProvider, DAO"> 
        <property name="ConnectionString" value="server=.\sql2k5;uid=sa;pwd=sa123;database=test;" /> 
    </object> 
    <object id="SpringDaoHelper" type="DAO.SpringDaoHelper"> 
        <property name="SessionFactory" ref="SessionFactory" /> 
    </object> 

</objects> 
web.config主要配置这三个,其中<httpModules>配置在system.web节点内
<spring> 
        <context> 
            <resource uri="config://spring/objects"/> 
            <resource uri="assembly://SpringX/SpringX/spring_hibernate_dao.xml"/> 
        </context> 
        <objects xmlns="http://www.springframework.net"/> 
    </spring>
<httpModules> 
            <add name="Spring" type="Spring.Context.Support.WebSupportModule, Spring.Web"/> 
        </httpModules> 
        <httpHandlers> 
            <add verb="*" path="*.aspx" type="Spring.Web.Support.PageHandlerFactory, Spring.Web"/> 
        </httpHandlers>
<configSections> 
        <sectionGroup name="spring"> 
            <section name="context" type="Spring.Context.Support.WebContextHandler, Spring.Web"/> 
            <section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core"/> 
        </sectionGroup> 
        <section name="SpringOverrideProperty" type="System.Configuration.NameValueSectionHandler"/> 
        <section name="nhibernate" type="System.Configuration.NameValueSectionHandler, System, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/> 
    </configSections>
好了当你运行出现Nhibernate composite-id class must override Equals()时是因为当使用Nhibernate查询表中的数据时,如果表中存在复合主键,如果没有在相应的实体类中重写Equals(),那么将会出现异常。需要在实体类中重写Equals()方法,如果还出现错误,说是编译不了映射文件,那么再重载GetHashCode()


本文转自 BruceAndLee 51CTO博客,原文链接:http://blog.51cto.com/leelei/323027,如需转载请自行联系原作者

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值