We have entities that don't implement equals and hashCode since hibernate doesn't recommends this. Hibenate promises to only have one instance of entity of same type and id per session. This suggests that, when enities are retrived by hibernate, calling equals on entity of type A and id 1 will return true if called by entity of same type and id. If my thinking is wrong please correct me. If not, here is the case, that this does't work (everything is happening in same session).
Entity model: A references a collection of Bs by @OneToMany association. A and B are abstract classes with joined subclasses (InheritanceType.JOINED)
Scenario: 1. load B with id=1 => returns proxy of B 2. load A with id=1 => returns proxy of A with noninitialized PersistentSet of Bs 3. call getter for Bs on A => initializes PersistentSet of Bs and that persistent set contains real entities and not proxies (including B with id=1) 4. calling equals on B from step 1. with B from step 3. (the one with id=1) will always return false
Proxy of B from step 1. points to same B from step 3.
As much as I understand, the collections will not work without overriding equals and hashCode. But overriding equals and hashCode triggers select when entities are put to HashMaps and HashSets, which we would like to avoid. Is there somethig we don't do right?
|