• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Ron McLeod
  • Tim Cooke
Sheriffs:
  • Devaka Cooray
  • paul wheaton
  • Mark Herschberg
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Frits Walraven
  • Jj Roberts
Bartenders:
  • Carey Brown
  • salvin francis
  • Piet Souris

Overrided hashcode and equals for Privelege class but Set<> contains method still returning false

 
Ranch Hand
Posts: 652
2
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have below JPA Entity Privelege



But when I have an instance of Set<Privilege> on which I want to check if an PrivilegeId exists, I always gets false


In database I have 3 Privileges:
1 CAN_DELETE
2 CAN_ADD
3 CAN_EDIT

Of course, I can do this in another way by iterating over the Set<> and checking the value, but I wanted to try this way as well.

What I am doing wrong? Are equals() and hashcode() correct here?
 
Marshal
Posts: 72048
312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please explain how that equals() method works, particularly about the cast in line 16. It looks incorrect to me.
How are instances of your class created? Do you have a default constructor, or have you only shown part of the code?
 
Campbell Ritchie
Marshal
Posts: 72048
312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why are you seeking a Byte (not a byte) in the Set? You know the Set won't contain it.
 
Saloon Keeper
Posts: 12722
277
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As Campbell pointed out, there's a lot of wonkiness here.

1) You can't give different equality definitions for sub-classes. Override equals() once and make it final. The implementation should be based on the current class, not sub-classes. Use instanceof, not getClass().
2) equals() should account for ALL normalized properties of an object, not just a subset. If you want to compare a subset of an object's properties, don't use equals().
3) bytes are not Privileges. You're comparing apples to oranges.
4) It looks like you're using Project Lombok. Stop it. Lombok is a terrible substitute for writing proper type-safe code.
5) Most importantly, don't base your business logic on mutable DTOs. A DTO is only there to facilitate transferring data to and from the database. Convert it to an immutable instance of your business model, which will also contain the equals() implementation.

Now, even though Privilege has a correct equals() implementation, DON'T use it to compare privileges if you're only interested in the IDs of the privileges. Do this instead:
reply
    Bookmark Topic Watch Topic
  • New Topic