首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >NHibernate on DB2 session.get()抛出System.IndexOutOfRangeException

NHibernate on DB2 session.get()抛出System.IndexOutOfRangeException
EN

Stack Overflow用户
提问于 2011-09-26 17:58:29
回答 1查看 724关注 0票数 2

我正在学习NHibernate入门教程:“您的第一个基于NHibernate的应用程序”。我正在创建一个Product,然后使用Session.get()来证明我可以读取这个对象。

它在Server中运行良好,但当我尝试使用DB2时,我得到了一个异常。( Server Ce版本可以工作-也就是说。在这些版本之间有一些小的变化,比如int,而不是Id的GUID )。

我非常熟悉Hibernate和SQL数据库。这是我第一次接触NHibernate和DB2。(来自Java世界)。我希望有任何建议,尤其是那些在DB2上使用DB2的人(显然很少)。

抢夺

完全例外是

Examples.DB2.NHibernateExamples.Can_add_new_product抛出了异常: NHibernate.Exceptions.GenericADOException:未能加载实体: Examples.DB2.Domain.Product#1 --> System.IndexOutOfRangeException: DB2ParameterCollection与Count=0的无效索引0。

这发生在Get(.)调用以下代码:

代码语言:javascript
运行
复制
    [TestInitialize]
    public void TestInitialize()
    {
        TestFixtureSetup();
        SetupContext();
    }


    [TestMethod]
    public void Can_add_new_product()
    {
        var product = new Product { Id = 1, Name = "Apple", Category = "Fruits"};

        using (ISession session = _sessionFactory.OpenSession())
        {
            using (ITransaction transaction = session.BeginTransaction())
            {
                session.Save(product);
                transaction.Commit();
            }
        }

        using (ISession session = _sessionFactory.OpenSession())
        {
            //var query = session.CreateQuery("from Product");
            //var products = query.List<Product>();
            //Assert.AreEqual(1, products.Count);

            var fromDb = session.Get<Product>(product.Id);
            Assert.IsNotNull(fromDb);
            Assert.AreNotSame(product, fromDb);
            Assert.AreEqual(product.Name, fromDb.Name);
            Assert.AreEqual(product.Category, fromDb.Category);
        }
    }

    private void TestFixtureSetup()
    {
        _configuration = new Configuration();
        _configuration.Configure();
        _configuration.AddAssembly(typeof (Domain.Product).Assembly);
        _sessionFactory = _configuration.BuildSessionFactory();
    }

    private void SetupContext()
    {
        new SchemaExport(_configuration).Execute(true, true, false);
    }

异常似乎表明Id参数没有传递给DB2查询。如果在Get()之前取消对这三行的注释,它就会正常工作,因为行已经被缓存,Get()实际上并不会转到数据库。

以下是Product.cs的定义:

代码语言:javascript
运行
复制
using System;

namespace Examples.DB2.Domain
{
    class Product
    {
        public virtual int Id { get; set; }
        public virtual string Name { get; set; }
        public virtual string Category { get; set; }
        public virtual bool Discontinued { get; set; }
    }
}

以下是Product.hbm.xml:

代码语言:javascript
运行
复制
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   assembly="Examples.DB2"
                   namespace="Examples.DB2.Domain">

  <class name="Product">
    <id name="Id">
      <generator class="native" />
    </id>
    <property name="Name" />
    <property name="Category" />
    <property name="Discontinued" type="YesNo"/>
  </class>

</hibernate-mapping>

以下是hibernate.cfg.xml:

代码语言:javascript
运行
复制
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
  <session-factory>
    <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>

    <property name="dialect">NHibernate.Dialect.DB2Dialect</property>
    <property name="connection.driver_class">NHibernate.Driver.DB2Driver</property>
    <property name="connection.connection_string">Database=SAMPLE; UID=DEV; PWD=password</property>

    <property name="show_sql">true</property>
  </session-factory>
</hibernate-configuration>

我在VisualStudio2010PremiumonWindows7上工作,我使用的是DB2 Express-C9.7.4。

EN

回答 1

Stack Overflow用户

发布于 2011-11-07 22:48:46

好的,

我找到了答案。目前还不完全确定解决方案,但在NHibernate的最后版本中,他们添加了一个调用AdoNet\AbstractBatcher.cs,名为RemoveUnusedCommandParameters。此过程调用Driver.RemoveUnusedCommandParameters

我现在的解决方案只是注释掉这个调用,让函数什么也不做。

我将与nhuser组讨论这个问题,看看是否有更好的长期解决方案。

谢谢

dbl

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7559179

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档