NHibernate配置的问题
虽然看到已经有很多人问过这个问题了,但是我还是没办法懂,我的问题是这样的,我用的是VS2005,连接mysql5.0,第一次接触NHibernate,不知道配置文件的书写规范,在网上改了一个例子,配置文件的名称 hibernate.cfg.xml
<?xml version="1.0" encoding="utf-8"?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.0" >
<session-factory>
<property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
<property name="connection.connection_string">Database=localhost;Data Source=hbm;User Id=root;Password=root</property>
<property name="adonet.batch_size">10</property>
<property name="show_sql">true</property>
<property name="dialect">NHibernate.Dialect.MsSql2000Dialect</property>
<property name="use_outer_join">true</property>
<property name="command_timeout">10</property>
<property name="query.substitutions">true 1, false 0, yes 'Y', no 'N'</property>
<mapping assembly="DomainModel"/>
</session-factory>
</hibernate-configuration>
不知道对不对啊,我想nhhibernate应该有配置文件的书写规范吧,大家有没有这样的资料,能否给我共享下,掌握了就不会这么白了呵呵。
类文件
Cource.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace NHibernateTest
{
public class Course
{
public Course() { }
private int courseId;
private string courseName;
private DateTime dateCreated;
public int CourseId
{
get { return courseId; }
set { courseId = value; }
}
public string CourseName
{
get { return courseName; }
set { courseName = value; }
}
public DateTime DateCreated
{
get { return dateCreated; }
set { dateCreated = value; }
}
}
}
与之对应的xml Course.hbm.xml
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="NHibernateTest.Course, NHibernateTest" table="Course">
<id name="CourseId" column="CourseId" type="Int32">
<generator class="native" />
</id>
<property name="CourseName" column= "CourseName" type="string" length="40"/>
<property name="DateCreated" type="DateTime" length="8"/>
</class>
</hibernate-mapping>
调用的地方
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using NHibernate.Cfg;
using NHibernate;
namespace NHibernateTest
{
public partial class Form1 : Form
{
private static Configuration mCfg = new Configuration();
private static NHibernate.ISessionFactory SessionFactory = null;
public Form1()
{
InitializeComponent();
mCfg.AddXmlFile(@"D:\project\NhibernateTest\NhibernateTest" + @"\Course.hbm.xml");
// mCfg.AddXmlFile(@"D:\project\NhibernateTest\NhibernateTest" + @"\Student.hbm.xml");
SessionFactory = mCfg.BuildSessionFactory();
}
private void button1_Click(object sender, EventArgs e)
{
Configuration mCfg = new Configuration();
Student obj = new Student();
obj.StudentName = "wuyang";
obj.StudentPassword = "helloWorld";
ISession vSession = SessionFactory.OpenSession();
ITransaction vTransaction = vSession.BeginTransaction();
try
{
vSession.Save(obj);
vTransaction.Commit();
}
catch (Exception)
{
vTransaction.Rollback();
}
vSession.Close();
}
}
}
出错的地方
mCfg.AddXmlFile(@"D:\project\NhibernateTest\NhibernateTest" + @"\Course.hbm.xml");
报错信息:
Could not compile the mapping document: D:\project\NhibernateTest\NhibernateTest\Course.hbm.xml
呵呵。罗嗦了,请大家帮我看看这是什么错误啊,小弟先谢谢了
--------------------编程问答-------------------- 应该还有异常的内部信息,你再看一下innerexception --------------------编程问答-------------------- 你的Nhibernate是什么版本?最新的版本的话,你的配置文件有问题
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
改成
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
建议载个CodeSimith或者CoolCode自动生成hbm文件
--------------------编程问答-------------------- 恩,我的就是2.2的,那些连接的配置都没有问题么?
innerexception 是什么啊?怎么看啊
--------------------编程问答-------------------- 你按照2楼说的先把配制文件改过来...
然后再改一下nhibernate的配制文件
<session-factory>
<property name="dialect">NHibernate.Dialect.MySQLDialect</property>
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="connection.driver_class">NHibernate.Driver.MySqlDataDriver</property>
<property name="connection.connection_string">Server=server;Database=dbName;User ID=user;Password=passwd</property>
</session-factory>
然后注意上面的修改,dialect和connection.driver_class --------------------编程问答-------------------- 首先谢谢大家回帖,然后还有些小问题,
都已经改成2.2 了, 2.0 也都试过了,
然后再改一下nhibernate的配制文件
<session-factory>
<property name="dialect">NHibernate.Dialect.MySQLDialect </property>
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider </property>
<property name="connection.driver_class">NHibernate.Driver.MySqlDataDriver </property>
<property name="connection.connection_string">Server=server;Database=dbName;User ID=user;Password=passwd </property>
</session-factory>
然后注意上面的修改,dialect和connection.driver_class
这个也改了,问题没有解决,难道是数据库连接不上么
错误还是这个,Could not compile the mapping document:
应该不是数据库的问题吧
?
我的配置文件叫做 hibernate.cfg.xml 没有问题吧
或者改成App。Config才可以么?
--------------------编程问答-------------------- 还是我的Course的xml有问题?能不能给指·一下,呵呵 --------------------编程问答-------------------- <class name="NHibernateTest.Course, NHibernateTest" table="Course">
<id name="CourseId" column="CourseId" >
<generator class="native" />
</id>
<property name="CourseName" column= "CourseName">
<property name="DateCreated" column= "DateCreated">
</class>
--------------------编程问答-------------------- --------------------编程问答-------------------- 应该将配置信息放到App.Config中。
补充:.NET技术 , C#