We're updating the issue view to help you get more done. 

Best practice for equals/hashCode implementation

Description

When domain model contains even one lazy reference to an object, default equals fails when it's compared to a) the actual implementation returned by Session.get/load or b) other proxies (at least of different supertype). Overriding equals on a class that uses surrogate id is not that simple. However, there is a simple solution to this problem:

In domain class, override equals like this:

1 2 3 4 5 6 7 8 9 10 11 public boolean equals(Object obj) { return this == getImplementation(obj); } public static Object getImplementation(Object obj) { if (obj instanceof HibernateProxy) { return ((HibernateProxy) obj).getHibernateLazyInitializer().getImplementation(); } else { return obj; } }

This should result always in comparing object references of actual instances and thus preserve symmetry.

It's understandable that you don't wan to publish that kind of getImplementation utility e.g. in Hibernate, but maybe you could support this more directly by implementing

Hibernate.equals(Object o1, Object o2)

Environment

None

Status

Assignee

Unassigned

Reporter

SamppaS

Labels

None

Worked in

None

Feedback Requested

None

Feedback Requested By

None

backPortable

None

Community Help Wanted

None

Suitable for new contributors

None

Requires Release Note

None

Pull Request

None

backportDecision

None

backportReEvaluate

None

Components

Fix versions

Priority

Major