DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations

Tread Carefully With Entities Equality

Nicolas Fränkel user avatar by
Nicolas Fränkel
CORE ·
Jan. 03, 11 · Interview
Like (0)
Save
Tweet
Share
6.56K Views

Join the DZone community and get the full member experience.

Join For Free

I’ve barely begun Scala and I learned plenty of facts on Java (see previous article on a javac compiler quirk). Now, I’ve come upon another interesting point, this time completely unrelated to the compiler. It’s strange because now that I’ve realized it exists, it makes me nervous on how I could have ignored it before.

The core of the problem lies in mutable objects. IMHO, entities should be mutable because one can load one from the datastore, change it, then save changes back to the store.

In order to reveal it, let’s create a simple entity:

public class Person {

private Long id;

private String name;

// Getters and setters
...
}

Now, imagine the following entity:

public class Registry {

private Long id;

private Set<Person> persons;

// Getters and setters
...
}

Since we created a Set of persons, we have to both implement equals() and hashCode() for the Person class. Using our favorite IDE, we confidently generate both from the id and name attributes for a person should be equals to another only when all attributes are the same. Let’s test this behaviour:

Person person1 = new Person();
person1.setId(1L);
person1.setName("Joe");

Person person2 = new Person();
person2.setId(2L);
person2.setName("Jack");

Set<Person> persons = new HashSet<Person>();

persons.add(person1);
persons.add(person2);

person1.setName("William");

persons.add(person1);

Now guess the set’s size. Bad (but common) answer: 2. Good answer: 3. The reason is that setting the name changed person1‘s hash code, thus the set contains it twice.

For mutable objects, and more particularly entities, only ids (primary keys) should be accounted for when generating equals() and hashCode().

This may seem a simple advice but it will transform into a deadly bug if not headed. Unfortunately, this bug can lay dormant for some time if you design your entities collections with List (ordered collections) instead of Set: the former won’t call hashCode() nor equals().

To be sure, try to design your objects to be immutable (which probably won’t be the case for entities).

Sources for this article are available here in Maven/Eclipse format.

From http://blog.frankel.ch/tread-carefully-with-entities-equality

entity

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • How To Handle Secrets in Docker
  • Fargate vs. Lambda: The Battle of the Future
  • HTTP vs Messaging for Microservices Communications
  • Reliability Is Slowing You Down

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: