NHibernate 联合主键Could not compile the mapping document:

ddj1982 ddj1982 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
cs文件
using System;
using System.Collections;
using System.Web.UI.WebControls;

namespace test.module
{
#region TestDoubleKey

/// <summary>
/// TestDoubleKey object for NHibernate mapped table 'TestDoubleKey'.
/// </summary>
[Serializable]
public class TestDoubleKey
{
#region Member Variables

protected string _name;
private IdRecordPK idPK;

#endregion

#region Constructors

public TestDoubleKey() { }

public TestDoubleKey( string name )
{
this._name = name;
}

#endregion

#region Public Properties

public IdRecordPK IdPK
{
set { idPK = value; }
get { return idPK; }
}

public string Name
{
get { return _name; }
set
{
if ( value != null && value.Length > 10)
throw new ArgumentOutOfRangeException("Invalid value for Name", value, value.ToString());
_name = value;
}
}


public override bool Equals(object obj)
{
if(obj is TestDoubleKey)
{
TestDoubleKey secord = obj as TestDoubleKey;
if (this.IdPK.Test1 == secord.IdPK.Test1 && this.IdPK.Test2 == secord.IdPK.Test2)
{
return true;
}
else
return false;
}
return false;
}


public override int GetHashCode()
{
return base.GetHashCode();
}
#endregion
}

#endregion

[Serializable]
public class IdRecordPK
{
public IdRecordPK()
{
}

private int test1;
public int Test1
{
set { test1 = value; }
get { return test1; }
}

private int test2;
public int Test2
{
set { test2 = value; }
get { return test2; }
}
public override bool Equals(object obj)
{
return base.Equals(obj);
}

public override int GetHashCode()
{
return base.GetHashCode();
}

}
}
xml文件
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="test.module.TestDoubleKey, test.module" table="TestDoubleKey" lazy="false">
<composite-id>
<key-property name="test1" type="int">
<column name="test1" length="4"/>
</key-property>
<key-property name="test2" type="int">
<column name="test2" length="4"/>
</key-property>
</composite-id>
<property name="Name" type="String">
<column name="name" length="10" sql-type="nchar" not-null="false"/>
</property>
</class>
</hibernate-mapping>
测试web文件
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using NHibernate;
using NHibernate.Cfg;
using test.module;


public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{
NHibernate.Cfg.Configuration cfg = new NHibernate.Cfg.Configuration();
cfg.AddAssembly("test.module");----这儿报错(Could not compile the mapping document: test.module.TestDoubleKey.hbm.xml)

ISessionFactory factory = cfg.BuildSessionFactory();

TestDoubleKey testDoubleKey = new TestDoubleKey();


ITransaction trans = null;
ISession session = factory.OpenSession();
try
{
trans = session.BeginTransaction();
testDoubleKey.IdPK.Test1 = 1;
testDoubleKey.IdPK.Test2 = 1;
testDoubleKey.Name = "first";

session.Save(testDoubleKey);
trans.Commit();
TextBox1.Text = "success";
}
catch(Exception ex)
{
trans.Rollback();
TextBox1.Text = ex.Message;
}

}
}
那位高手帮帮小弟呀。555555555555555卡了两天了。
...全文

相关推荐

评论 11
收藏 已收藏
分享
返回顶部
ddj1982
.NET社区 62,272 成员 668,968 内容