Equals and HashCode

0

Hello! I have a conceptual question. I have a readDao (table) method inside the ObjectDAO (Bank Access) method that returns a HashSet named ResultRead with all objects in the database of that table . I also have an insertRule (Object obj) method inside an ObjectRule (business rule), which executes an ObjectDAO method, after doing the following verification:

 if (!resultadoRead.contains(obj)) {

 // executa a inserção do objeto no banco de dados.
 ObjectDAO.insertDAO(obj);
 }

My question is: For the contains function, do I need to implement the hashset and equals in the Object or in the DAO Class? because in the DAO class, there are no attributes, only methods. The attributes belong to the Object.

Do I use inheritance? another form of verification? I abandon java because this doubt is very stupid?

    
asked by anonymous 21.10.2017 / 23:07

1 answer

1

In theory you would not need to override these methods for the contains to work. By default, contains() will call the equals() of the passed object per parameter which in turn will use hashCode() to perform the comparison. Again, in theory this should already be working. If you really need to change the behavior of these methods, you need to make the change inside the object structure and not in your DAO class, after all it is the object being compared.

    
22.10.2017 / 00:43