1

I have entity class:

    [Serializable, Class(Table = "mon.tableView", Lazy = false)]
    public class TableView
    {
    [CompositeId(1)]
    [KeyProperty(2, Name = "column1", Column = "column1", TypeType = typeof(int))]
    [KeyProperty(3, Name = "column2", Column = "column2", TypeType = typeof(int))]
    internal int VirtualId { get; set; }

    [Property(Column = "column1")]
    private int column1 { get; set; }

    [Property(Column = "column2")]
    public int? column2 { get; set; }

    [Property(Column = "column3")]
    public int column3 { get; set; }

    [Property(Column = "otherColumn")]
    public string otherColumn{ get; set; }

    public override bool Equals(object other)
    {
        if (this == other)
            return true;

        TableViewv dp = other as TableView;

        if (vdp == null)
            return false; // null or not a ReadOnlyUserRole

        return column1.Equals(vdp.column1) ^ column2.Equals(vdp.column2) && column3.Equals(vdp.column3) && otherColumn.Equals(vdp.otherColumn);        }

    public override int GetHashCode()
    {
        return column1.GetHashCode() ^ column2.GetHashCode();
    }
}

I know that GetHashCode() give 2 answers: - when 2 objects are not equal we know that are NOT equal. - when 2 objects are equal they might be equal but it's not for sure. Therefore there is Equal() method for this.

GetHashCode() method are giving me the same integer for 2 objects but I know other properties are not equal. When I get the list these objects i have few times duplicated object and my question is when Equals() method is called? Because I have never seen in debug mode when this method was called.

maciusik
  • 103
  • 1
  • 1
  • 9

1 Answers1

0

You GetHashCode implementation looks correct, but your equals seems to have a problem, you should only compaire the values of the compositkeys, column1 and column2 (nullable):

public override bool Equals(object other)
{
    if (this == other)
        return true;

    TableViewv dp = other as TableView;

    if (vdp == null)
        return false; // null or not a ReadOnlyUserRole


    return column1 == vdp.column1 &&
            ( (!column2.HasValue && !vdp.column2.HasValue ) || (column2.HasValue && vdp.column2.HasValue && column2.Value == vdp.column2.Value ));    
}
Peter
  • 27,590
  • 8
  • 64
  • 84
  • Thanks for advice:) But my main point is when Equals() method is called and is there any difference between .Net GetHashCode() method and Nhibernate GetHashCode() ? I have never seen calling to Eqals() method when 2 Objects had the same hashcode. – maciusik May 23 '13 at 08:46
  • NHibernate is depending on the Equals, if you do implement it wrong NHibernate will not work correctly. For example if you you query the same object twice (same object for NHibernate has the same fkey), it is depending on your Equals implementation. GetHashCode is no substitute for the Equals method, and it will be called when you have two objects with the same HashCode. – Peter May 23 '13 at 09:38