How do you get equals() working with Hibernate entities when lazy=true?

For some reason when lazy=true on all my entities, the equals() method does not work correctly when one side is a lazily-loaded entity and the other side is a normal entity. Here's an example:

if(activeTask.getTask().equals(task)) {...}

In this case, the activeTask's task will be a proxy while the right side will be a regular one. The equals() will fail. To fix this problem, I often do things like this:

if(activeTask.getTask().getId() == task.getId()) {...}

This works, but it's not ideal. I'd rather use my equals() method.

Does anyone have a nice solution to this problem? It really adds to the application's level of noise to have to think about stuff like this.

If I say lazy=false, I don't have to deal with proxy's, and so equals() will work. But this has a very negative impact on performance.

It is just not cool to have to say, "equals() works in all cases, except when you use proxies... then equals() is not reliable."

13
задан egervari 30 November 2010 в 01:04
поделиться