• Nhibernate compositeid class must override Equals()解决办法


    概述:

       当使用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了.

  • 相关阅读:
    spring boot SpringApplication.run 执行过程
    算法 计算四则运算字符串结果
    算法 RingBuffer
    java BigDecimal 四舍五入
    算法 常用函数和近似
    java 多线程执行
    Java 三个线程依次输出ABC
    Java interrupt 中断
    java 垃圾收集器与内存分配策略
    软件项目与软件产品的区别
  • 原文地址:https://www.cnblogs.com/abcdwxc/p/1397360.html
Copyright © 2020-2023  润新知