Is there any reason to override Equals for an Entity?

An entity has identity, which implies there should only be one object (e.g. a particular customer) in memory.

As such, reference equality (which is what the base version of object.equals checks for) should be the only equality we care about.

A value object on the other hand would need to overrride Equals as there may be many in memory.

Are there any issues with the above reasoning?

I just can’t imagine why I would have two customer objects representing the same customer, and in any case where I did (e.g. took a clone to do something with it), I can’t think of any circumstances where I would need to check if it was equal to the orginal object.

Even in a distributed environment, any client process should only contain one object for a certain customer.

1

I just can’t imagine why I would have two customer objects representing the same customer, and in any case where I did (e.g. took a clone to do something with it), I can’t think of any circumstances where I would need to check if it was equal to the orginal object.

This can easily happen: You read a list of objects from somewhere (e.g., from the database) and receive one object from elsewhere (e.g., from a web request). Now you don’t call equals, you look up the object in the list, so equals gets called anyway.

That’s where a field based equals makes sense. However, selecting the fields which should participate in the checks is something between hard and impossible. Consider class Person, what business property never changes? Name? Surely not. SSN? Neither.

That’s why many prefer an ID-based equality. This has other problems, e.g., assigning the ID to newly created objects and some others. There’s lengthy discussion about it somewhere on Hibernate pages.

Not defining equals is a way I’ve never heard about.

Even in a distributed environment, any client process should only contain one object for a certain customer.

What if one process sends an entity to another one?

3

I just can’t imagine why I would have two customer objects
representing the same customer

Not all objects represent “customers”. Other objects represent other kind of entities. In those cases a value equality could be more or less useful according to bussines rules, allowing to be able to check whether or not a given object passed as a parameter is already (business-based) in a given collection.

Even in a distributed environment, any client process should only contain one object for a certain customer.

Distributed systems are not centralized. They are the opposite of centralized. They interoperate sending objects back and forth. A value equality helps in such interoperability since, being distributed, memory location addresses wouldn’t match. Also how would you prevent independent, distributed processes from instantiating objects that are the same according to a business rule ?

Again, not all objects are customers and your question is about “entities”. Maybe you should rename your question to “Is there any reason to override equeals for a customer class?”

Another issue.

According to your reasoning two separate customer objects, with identical state ( values ) would be different customers since they will have different memory addresses (variable references).

Never assume your inability to imagine why someone would WANT to do something has any correlation as to what someone else WILL do.

Remember Murphy’s Law, and ask “is there ANY situation where not being able to recognize distinct objects as equal will cause problems”. If the answer is yes, the few lines of code needed to compare equality by value will be worthwhile.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật