NHibernate入门

时间:2009-06-19 21:34:20

标签: nhibernate

我正在尝试在NHibernate中开发我的Hello World程序。

我的代码如下:

MyClass.cs
----------
using System.Collections.Generic;
using System.Text;
using System;
using NHibernate.Collection;
using NHibernate.Mapping;
using Iesi.Collections;

namespace NHibernate__MyClass
{
    public class MyClass 
    {
        int id;
        string name;
        int _value;

        public MyClass()
        {
            id = 0;
            name = "";
            _value = 0;
        }

        public virtual int Id
        {
            get { return id; }
            set { id= value; }
        }

        public virtual string Name
        {
            get { return name; }
            set { name= value; }
        }

        public virtual int Value
        {
            get { return _value; }
            set { _value= value; }
        }
    }
}


MyClass.hbm.xml
---------------
<?xml version="1.0" encoding="utf-8" ?>

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="NHibernate__MyClass" assembly="NHibernate__MyClass">
  <class name="MyClass" table="MyClass">
    <id name="Id">
      <column name="ID" sql-type="int" not-null="true"/>
      <generator class="native" />
    </id>
    <property name="Name">
      <column name="Name" not-null="true" />
    </property>
    <property name="Value">
      <column name="Value" not-null="true" />
    </property>
  </class>
</hibernate-mapping>

Program.cs
----------
using System;
using System.Collections.Generic;
using System.Text;

using NHibernate;
using NHibernate.Cfg;

namespace NHibernate__MyClass
{
    class Program
    {
        static void Main(string[] args)
        {
            MyClass myClass = new MyClass();
            myClass.Id = 1;
            myClass.Name = "Hello World!";
            myClass.Value = 100;

            ISessionFactory sessionFactory = new Configuration().Configure().BuildSessionFactory();
            ISession session = sessionFactory.OpenSession();

            session.BeginTransaction();
            session.Save(myClass);
            session.Transaction.Commit();

            Console.ReadLine();
        }
    }
}

App.config
----------
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section
    name="hibernate-configuration"
    type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate"
/>
  </configSections>

  <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
    <session-factory>
      <property name="dialect">NHibernate.Dialect.MsSql2000Dialect</property>
      <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
      <property name="connection.connection_string">Data Source=.\SQLEXPRESS;Initial Catalog=NHibernate;Integrated Security=True</property>
      <mapping assembly="NHibernate__MyClass" />
    </session-factory>
  </hibernate-configuration>
</configuration>


SQL Table
---------
CREATE TABLE [dbo].[MyClass](
    [ID] [int] NOT NULL,
    [Name] [varchar](50) NOT NULL,
    [value] [int] NOT NULL
) ON [PRIMARY]

但是这个程序正在产生一个异常:

异常消息:{“无法插入:[NHibernate__MyClass.MyClass] [SQL:INSERT INTO MyClass(Name,Value)VALUES(?,?);选择SCOPE_IDENTITY()]”}

内部异常消息:{“无法将值NULL插入列'ID',表'NHibernate.dbo.MyClass';列不允许空值.INSERT失败。\ r \ n语句已终止。”} < / p>

可能是什么问题?

NHibernate DLL版本= 2.0.0.2002

1 个答案:

答案 0 :(得分:3)

由于映射文件中的此标记

<generator class="native" />

在SQL中,您需要将该表中的ID字段设置为标识。

您也可以让nHibernate生成标识字段。