Could not compile the mapping document: NHDemo.DomainModel.Demo.hbm.xml
开发工具vs2005打过sp1补丁.nhibernate 2.1.0。
项目架构:WebUI,NHDemo.BLL,NHDemo.DAL,NHDemo.DomainModel。
NHDemo.DomainModel:Demo.cs,Demo.hbm.xml
Demo.cs
- C# code
using System;using System.Collections.Generic;using System.Text;namespace NHDemo.DomainModel{ public class Demo { public Demo() { } #region private string _demoid; private string _title; public virtual string DemoID { set { _demoid = value; } get { return _demoid; } } public virtual string Title { set { _title = value; } get { return _title; } } #endregion }}
Demo.hbm.xml
- XML code
<?xml version="1.0" encoding="utf-8" ?><hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-lazy="false"> <class name="NHDemo.DomainModel.Demo, NHDemo.DomainModel" table="WF_DEMO" lazy="false"> <id name="DemoID" column="DEMOID" type="String" length="50"> <generator class="assigned" /> </id> <property name="Title" column="TITLE" type="String" length="500"/> </class></hibernate-mapping>
NHDemo.DAL:
SessionFactory.cs
- C# code
using System;using System.Collections.Generic;using System.Text;using NHibernate;using NHibernate.Cfg;using NHibernate.Tool.hbm2ddl;namespace NHDemo.DAL{ public class SessionFactory { public SessionFactory() { } private static ISessionFactory sessions; private static Configuration cfg; static readonly object padlock = new object(); public static ISession OpenSession(string AssemblyName) { if (sessions == null) { lock (padlock) { if (sessions == null) { BuildSessionFactory(AssemblyName); } } } return sessions.OpenSession(); } private static void BuildSessionFactory(string AssemblyName) { cfg = new Configuration(); cfg.AddAssembly(AssemblyName); //cfg.Configure(ser); sessions = cfg.BuildSessionFactory(); } }}
EntityControl.cs:
- C# code
using System;using System.Collections.Generic;using System.Collections;using System.Text;using NHibernate;namespace NHDemo.DAL{ public class EntityControl { public EntityControl() { } private static EntityControl entity; private string _AssemblyName; static readonly object padlock = new object(); public static EntityControl CreateEntityControl(string AssemblyName) { if (entity == null) { lock (padlock) { if (entity == null) { entity = new EntityControl(); entity._AssemblyName = AssemblyName; } } } return entity; } public void AddEntity(Object entity) { ISession session = SessionFactory.OpenSession(_AssemblyName); ITransaction transaction = session.BeginTransaction(); try { session.Save(entity); transaction.Commit(); } catch (Exception ex) { transaction.Rollback(); throw ex; } finally { session.Close(); } } public void UpdateEntity(Object entity, Object key) { ISession session = SessionFactory.OpenSession(_AssemblyName); ITransaction transaction = session.BeginTransaction(); try { session.Update(entity, key); transaction.Commit(); } catch (Exception ex) { transaction.Rollback(); throw ex; } finally { session.Close(); } } public void DeleteEntity(Object entity) { ISession session = SessionFactory.OpenSession(_AssemblyName); ITransaction transaction = session.BeginTransaction(); try { session.Delete(entity); transaction.Commit(); } catch (Exception ex) { transaction.Rollback(); throw ex; } finally { session.Close(); } } public IList GetEntities(string strHQL) { IList lst; ISession session = SessionFactory.OpenSession(_AssemblyName); ITransaction transaction = session.BeginTransaction(); lst = session.CreateQuery(strHQL).List(); transaction.Commit(); session.Close(); return lst; } }}
DemoDAL:
- C# code
using System;using System.Collections.Generic;using System.Collections;using System.Text;using NHDemo.DomainModel;namespace NHDemo.DAL{ public class DemoDAL { private EntityControl control; public DemoDAL() { control = EntityControl.CreateEntityControl("NHDemo.DomainModel"); } public void AddDemo(Demo demo) { control.AddEntity(demo); } public void UpdateDemo(Demo demo, string Id) { control.UpdateEntity(demo, demo.DemoID); } public void DeleteDemo(Demo demo) { control.DeleteEntity(demo); } public IList GetAllDemos(string strHQL) { return control.GetEntities(strHQL); } }}
NHDemo.BLL:
DemoBLL.cs:
using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using NHDemo.DAL;
using NHDemo.DomainModel;
namespace NHDemo.BLL
{
public class DemoBLL
{
private DemoDAL dal;
public DemoBLL()
{
dal = new DemoDAL();
}
public void AddDemo(Demo demo)
{
dal.AddDemo(demo);
}
public void UpdateDemo(Demo demo, string Id)
{
dal.UpdateDemo(demo, Id);
}
public void DeleletDemo(Demo demo)
{
dal.DeleteDemo(demo);
}
public IList GetAllDemos(string strHQL)
{
return dal.GetAllDemos(strHQL);
}
}
}
还有就webui的调用了,只是获取一个列表操作
string strHql = "from Demo";
DemoBLL bll = new DemoBLL();
IList lst = bll.GetAllDemos(strHql);
this.GridView1.DataSource = lst;
this.GridView1.DataBind();
webconfig中配置:
- XML code
<configSections> <section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler,NHibernate"/> </configSections> <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2"> <session-factory name="WebApp"> <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property> <property name="connection.connection_string">User ID=frameportal;Password=frameportal;Data Source=orcl</property> <property name="dialect">NHibernate.Dialect.OracleDialect</property> <property name="connection.driver_class">NHibernate.Driver.OracleClientDriver</property> <mapping assembly="NHDemo.DomainModel"/> </session-factory> </hibernate-configuration>
[解决办法]
是不是配置文件路径问题