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

ddj1982 2008-07-07 03:09:46
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卡了两天了。
...全文
681 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
Elvis_Zhou 2008-11-17
  • 打赏
  • 举报
回复
看了很多,但好像没有完全的解决方法。。。我怀疑也是版本问题。。。
iwant123 2008-11-14
  • 打赏
  • 举报
回复
我的已解决。问题不在我上面贴出的内容上,而是系统的.Config中<nhibernate>中的内容没被读入Configuration中。
《联合主键could not compile the mapping》 (http://topic.csdn.net/u/20080707/15/D8529056-D674-4A57-84D0-6E57EA63F994.html) 也是这个问题的,其他的原因可看看那里。
孤独侠客123 2008-11-14
  • 打赏
  • 举报
回复
<composite-id name="IdPK" class="test.module.IdRecordPK,test.module">
winner2050 2008-11-14
  • 打赏
  • 举报
回复
.net 的世界里面,这个属于冷门。
iwant123 2008-11-13
  • 打赏
  • 举报
回复
我也在问,还设答案
ddj1982 2008-07-07
  • 打赏
  • 举报
回复
真的没有人知道吗??这儿难道都用dataset的??
ddj1982 2008-07-07
  • 打赏
  • 举报
回复
跪求高手来帮我看看呀
ddj1982 2008-07-07
  • 打赏
  • 举报
回复
谁能帮帮我呀?
yuanzh 2008-07-07
  • 打赏
  • 举报
回复
还真没用过
帮你顶
关注
ddj1982 2008-07-07
  • 打赏
  • 举报
回复
那位大哥过来帮帮我啊
ddj1982 2008-07-07
  • 打赏
  • 举报
回复
没人知道吗?

61,826

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧