I am trying to override the mentioned methods for my `HashSet`:

```
Set<MyObject> myObjectSet = new HashSet<MyObject>();

```

**MyObject:**

```
public class MyObject implements Serializable {

  private static final long serialVersionUID = 1L;

  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  Long id;
  String name;
  int number;
  Map<String,String> myMap;

  public MyObject(String name, int number, Map<String,String> myMap) {
    this.name = name;
    this.number = number;
    this.myMap = myMap;
  }

  [...]
}

```

---

How do I override the hashcode(), equals() and compareTo() method? 

---

Currently I have the following:

```
public int hashCode () {
  return id.hashCode();
}

// override the equals method.
public boolean equals(MyObject s) {
  return id.equals(s.id);
}

// override compareTo
public int compareTo(MyObject s) {
  return id.compareTo(s.id);
}    

```

I read that comparing by id is not enough this is object is a persistent entity for the DB (see [here](http://docs.jboss.org/hibernate/core/4.0/manual/en-US/html/persistent-classes.html#persistent-classes-equalshashcode)).

The name and number aren't unique across all objects of this type. 

So how should I override it?  

Do I also need to compare the hashMap inside it? 

I am confused. The only unique thing about the object is the the map myMap which gets populated later in the lifecycle. 

How do I check for its equality?

Based on all the responses I have changed the methods to the following

```
 @Override
    public boolean equals(final Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;

    final MyComplexObj myComplexObj = (MyComplexObj) o;

    return myMap != null ? myMap.equals(myComplexObj.myMap) : myComplexObj.myMap == null;
    }

    @Override
    public int hashCode() {
    return myMap != null ? myMap.hashCode() : 0;
    }

    public int compareTo(MyComplexObj o) {
    return myMap.compareTo(o.getMyMap()));
    }

```

This fails at the compareTo method, "this method is undefined for the type Map