Nhibernate composite-id class must override Equals()解决办法
2009-02-24 16:46
477 查看
概述:
当使用Nhibernate查询表中的数据时,如果表中存在复合主键,如果没有在相应的实体类中重写Equals(),那么将会出现"Could not compile the mapping document: TestCleanSnow.JkptGlxtUser.hbm.xml".当跟踪出错的内部信息的时候会发现,内部异常为"composite-id class must override Equals(): TestCleanSnow.JkptGlxtUserId"
解决办法:
在JkptGlxtUserId.cs这个实体类中重写Equals()方法如下:
public override bool Equals(object obj)
{
if (obj is JkptGlxtUser)
{
JkptGlxtUser gu = obj as JkptGlxtUser;
if (this.Operatorid == gu.Id.Operatorid
&& this.Orgid == gu.Id.Orgid)
{
return true;
}
else return false;
}
return false;
}
再次执行调用语句,发现还出现"Could not compile the mapping document: TestCleanSnow.JkptGlxtUser.hbm.xml".这个错误,但是跟踪内部异常为:InnerException = {"composite-id class must override GetHashCode(): TestCleanSnow.JkptGlxtUserId"}
到此很明显,在遇到复合主键的情况下,应该还得在JkptGlxtUserId.cs中重写GetHashCode(),代码如下:
public override int GetHashCode()
{
return Orgid.GetHashCode();
}
再次运行调用程序,OK了.
当使用Nhibernate查询表中的数据时,如果表中存在复合主键,如果没有在相应的实体类中重写Equals(),那么将会出现"Could not compile the mapping document: TestCleanSnow.JkptGlxtUser.hbm.xml".当跟踪出错的内部信息的时候会发现,内部异常为"composite-id class must override Equals(): TestCleanSnow.JkptGlxtUserId"
解决办法:
在JkptGlxtUserId.cs这个实体类中重写Equals()方法如下:
public override bool Equals(object obj)
{
if (obj is JkptGlxtUser)
{
JkptGlxtUser gu = obj as JkptGlxtUser;
if (this.Operatorid == gu.Id.Operatorid
&& this.Orgid == gu.Id.Orgid)
{
return true;
}
else return false;
}
return false;
}
再次执行调用语句,发现还出现"Could not compile the mapping document: TestCleanSnow.JkptGlxtUser.hbm.xml".这个错误,但是跟踪内部异常为:InnerException = {"composite-id class must override GetHashCode(): TestCleanSnow.JkptGlxtUserId"}
到此很明显,在遇到复合主键的情况下,应该还得在JkptGlxtUserId.cs中重写GetHashCode(),代码如下:
public override int GetHashCode()
{
return Orgid.GetHashCode();
}
再次运行调用程序,OK了.
相关文章推荐
- NHibernate composite-id class must override Equals() 解决办法
- NHibernate composite-id class must override Equals() 解决办法 分类: 技术心得(C#与JAVA) 2013-01-23 15:20 1289人阅
- NHibernate composite-id class must override Equals() 解决办法
- nhibernate:composite-id class must override Equals()
- Android Your content must have a ListView whose id attribute is 'android.R.id.list'错误的解决办法
- python2.7 使用super关键词 报错 TypeError: must be type, not classobj 解决办法
- There is no getter for property named 'id' in 'class java.lang.Integer'问题解决办法
- python2.7 使用super关键词 报错 TypeError: must be type, not classobj 解决办法
- idhttp post 出现exception class EIdSocketError with message 'Socket Error # 10054的解决办法
- Apple Mach-O Linker (id) Error "_OBJC_CLASS...错误解决办法 Apple Mach-O Linker (id) Error "_OBJC_CLASS...错误解决办法
- org.apache.hive.com.esotericsoftware.kryo.kryoexception: encountered unregistered class id 错误解决办法
- Your content must have a ListView whose id attribute is 'android.R.id.list'解决办法
- Android中 Your content must have a ListView whose id attribute is 'android.R.id.list'错误的解决办法
- Your content must have a TabHost whose id attribute is 'android.R.id.tabhost' 问题,写了id语句仍出错的解决办法
- must register a nib or a class for the identifier or connect a prototype cell in a storyboard'的解决办法
- u410设置4g休眠分区id=84 override格式不正确的解决办法 [
- Android—Android中 Your content must have a ListView whose id attribute is 'android.R.id.list'错误的解决办法
- Your content must have a ListView whose id attribute is 'android.R.id.list'解决办法。
- ProbeException解决办法:There is no WRITEABLE property named 'id' in class 'java.lang.String'
- 工作札记--“ids for this class must be manually assigned before calling save”解决办法