Recommendation for using equals in Entities and avoiding LazyInitializationExceptions?
Posted
by huxendupsel
on Stack Overflow
See other posts from Stack Overflow
or by huxendupsel
Published on 2010-05-25T14:53:25Z
Indexed on
2010/05/28
7:01 UTC
Read the original article
Hit count: 154
In the beginning there is a problem that wants to be solved. In my case i got an LazyInitializationException while using indexof in a Collection to retrieve an Object for manipulation. Here i start to think about using equals in EntityBeans (OR-Mapper at all). I know there are some discussions about overriding equals in association with OR-Mapper as hibernate like
[1] Entities equals(), hashCode() and toString(). How to correctly implement them?
[2] To equals and hashcode or not on entity classes, that is the question.
I currently have some entities which implements the equals but inside the code i could not use equals several times because of the LazyInitializationExceptions. So i had to workaround and use eg. the name property of the object to identify it's equality. From my point of view the whole 'LazyInitializationException-thing' is not really mentioned in this questions.
I'd like to know have you got some good patterns or real live recommendations how to avoid such exception in an equal-Method. Shall i use some helper Methodes to distinguish if a Object of a class is already initialized (4) or should i apdicate the use of equals and use helper classes instead (2)? And what is about catching LazyInitializationExceptions in the equals?
[Edit]: If you put equals in context with the initialization of the Object then it will gain importance. Sometimes it is nessesary to have the Object fully initialized but sometimes you don't want to. Because you just need the Object itself (name, id, ...) not its Collection-Properties. So just for equalization you have to reattach the Object and load the whole bunch you don't realy need? Are there any other solutions for such a problem?
© Stack Overflow or respective owner