NHibernate 1.2.0 问题
/*insert license info here
*/
using System;
namespace Test.Model
{
/// <summary>
/// Generated by MyGeneration using the Serdar's NHibernate Object Mapping 1.1 template (based on Gustavo's) - serdar@argelab.net
/// </summary>
[Serializable]
public class Person
{
#region Private Members
private bool _isChanged;
private bool _isDeleted;
private int _id;
private string _name;
#endregion
#region Default ( Empty ) Class Constuctor
/// <summary>
/// default constructor
/// </summary>
public Person()
{
_id = 0;
_name = null;
}
#endregion // End of Default ( Empty ) Class Constuctor
#region Public Properties
/// <summary>
///
/// </summary>
public virtual int Id
{
get { return _id; }
set { _isChanged |= (_id != value); _id = value; }
}
/// <summary>
///
/// </summary>
public virtual string Name
{
get { return _name; }
set
{
if ( value != null )
if( value.Length > 50)
throw new ArgumentOutOfRangeException("Invalid value for Name", value, value.ToString());
_isChanged |= (_name != value); _name = value;
}
}
/// <summary>
/// Returns whether or not the object has changed it's values.
/// </summary>
public virtual bool IsChanged
{
get { return _isChanged; }
}
/// <summary>
/// Returns whether or not the object has changed it's values.
/// </summary>
public virtual bool IsDeleted
{
get { return _isDeleted; }
}
#endregion
#region Public Functions
/// <summary>
/// mark the item as deleted
/// </summary>
public virtual void MarkAsDeleted()
{
_isDeleted = true;
_isChanged = true;
}
#endregion
#region Equals And HashCode Overrides
/// <summary>
/// local implementation of Equals based on unique value members
/// </summary>
public override bool Equals( object obj )
{
if( this == obj ) return true;
if( ( obj == null ) || ( obj.GetType() != this.GetType() ) ) return false;
Person castObj = (Person)obj;
return ( castObj != null ) &&
( this._id == castObj.Id );
}
/// <summary>
/// local implementation of GetHashCode based on unique value members
/// </summary>
public override int GetHashCode()
{
int hash = 57;
hash = 27 * hash * _id.GetHashCode();
return hash;
}
#endregion
}
}
影射文件
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="Test.Model.Person, Test.Model" table="Person">
<id name="Id" type="Int32" unsaved-value="0">
<column name="id" sql-type="int" not-null="true" unique="true" index="PK_Person"/>
<generator class="native" />
</id>
<property name="Name" type="String">
<column name="name" length="50" sql-type="varchar" not-null="true"/>
</property>
</class>
</hibernate-mapping>
using System;
using System.Collections.Generic;
using System.Text;
using NHibernate;
using NHibernate.Cfg;
using Test.Model;
using NHibernate.Expression;
using NHibernate.Exceptions;
namespace TestHibernate
{
class Program
{
static void Main(string[] args)
{
Configuration config = new Configuration().AddAssembly("Test.Model");
ISessionFactory factory = config.BuildSessionFactory();
ISession session = factory.OpenSession();
Person person = new Person();
person.Name = "Jackie chan";
ITransaction tran = session.BeginTransaction();
try
{
session.Save(person );
tran.Commit();
Console.WriteLine("Insert Success!");
}
catch (Exception ex)
{
tran.Rollback();
Console.WriteLine(ex.Message );
}
}
}
}
配置文件
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2" >
<session-factory name="NHibernate.Test">
<!-- properties -->
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
<property name="show_sql">false</property>
<property name="dialect">NHibernate.Dialect.MsSql2000Dialect</property>
<property name="use_outer_join">true</property>
<!-- mapping files -->
<!-- <mapping assembly="Novelty.Model" /> -->
</session-factory>
</hibernate-configuration>
抱错信息::
Could not compile the mapping document: Test.Model.Person.hbm.xml
运行环境::
VS2005 SQLServer 2000 NHibernate 1.2.0 --------------------编程问答-------------------- 自己顶 --------------------编程问答-------------------- 很明细的报错,不能编译Test.Model.Person.hbm.xml。
应该是Test.Model.Person.hbm.xml文件有问题,看看你的Test.Model.Person.hbm.xml。
然后确定你的Test.Model.Person.hbm.xml文件已经做为嵌入的资源进行编译了 --------------------编程问答-------------------- 没有碰到过此问题
也许,你的 Test.Model.Person.hbm.xml 被其他程序以独占方式打开了 .... --------------------编程问答-------------------- Test.Model.Person.hbm.xml
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="Test.Model.Person, Test.Model" table="Person">
<id name="Id" type="Int32" unsaved-value="0">
<column name="id" sql-type="int" not-null="true" unique="true" index="PK_Person"/>
<generator class="native" />
</id>
<property name="Name" type="String">
<column name="name" length="50" sql-type="varchar" not-null="true"/>
</property>
</class>
</hibernate-mapping> --------------------编程问答-------------------- Test.Model.Person.hbm.xml不能被当前环境使用,你用监控看看是否有其他进程使用该资源 --------------------编程问答-------------------- 没有呀 --------------------编程问答-------------------- Test.Model.Person.hbm.xml有没有作为嵌入的资源 --------------------编程问答-------------------- 是嵌入资源呀 --------------------编程问答-------------------- 会不会xml的语法之类的有问题? --------------------编程问答-------------------- 不太清楚
配置文件是这样的:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="Test.Model.Person, Test.Model" table="Person">
<id name="Id" type="Int32" unsaved-value="0">
<column name="id" sql-type="int" not-null="true" unique="true" index="PK_Person"/>
<generator class="native" />
</id>
<property name="Name" type="String">
<column name="name" length="50" sql-type="varchar" not-null="true"/>
</property>
</class>
</hibernate-mapping>
--------------------编程问答-------------------- <class name="Test.Model.Person, Test.Model" table="Person">
Test.Model.Person是类的全名,从你写的看类名是Person,所在命名空间是Test.Model
Test.Model的地方应该是Person类所在的程序集名
检查一下Test.Model是不是程序集名,没人会给程序集起这样的名字吧
其他的代码没仔细看
Could not compile the mapping document: Test.Model.Person.hbm.xml
出现这个错误一般是此配置没作为嵌入的资源,还有就是这个文件有错 --------------------编程问答-------------------- 再改成这样试试
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="Test.Model.Person, Test.Model" table="Person">
<id name="Id" column="id" type="Int32" unsaved-value="0">
<generator class="native" />
</id>
<property name="Name" column="name" type="String" />
</class>
</hibernate-mapping> --------------------编程问答-------------------- 帮顶~ --------------------编程问答-------------------- ding
--------------------编程问答-------------------- 请问楼主解决了这个问题没有,我也碰到了这个问题,请指教!
补充:.NET技术 , C#