Equals / Hashcode

HI,

Just wanted to know why the java code generated does not include a default hashcode and equals method implementation to be used in comparisons (hibernate).

Is there a way to modify a template so it does?

Thanks
JP

Hi JP,

Thanks for your post. When you have more than 1 PK or define any business key in a table, you will get the code generated with hashcode and equals method implementation.

Hibernate will use equals() to check equal, if there is 1 PK only and auto generate id, all new objects will have id = 0 and equals() returns true. They will be treated as the same object but actually they are not.

If there is more than 1 PK (that means id must be manually defined), or there is business key defined (which is not id, and values should be filled manually and assumed unique), then equals() works fine for new objects, because they’re filled with different values manually.

Hashcode is with the same principle as equals.

BTW, you can define business key inside the Class Specification dialog (see image).

Hope this answers your question. If there is any further inquiry, please feel free to contact me.

Best regards,
Lilian Wong
business_key.PNG