Hi!
Even it is an old posting, I think it is still up to date. I have read the very good description about hashCode() and equals() in 'Java Persistence with Hibernate' and understand why to use the business key.
However I see two problems with it:
As described above the usage of the business key can make from an lazy association (very useful!) an non-lazy one which means additional DB calls.
The second problem is in a high concurrent system, an access like A.getB().size() can leads into an ObjectNotFoundException. Why? For filling the Set of Bs, the Set uses B.hashCode(). If this hashCode() depends on an other association with B1 and B1 is part of the business key of B, B1 must be loaded for B.hashCode(). If between the select for the Bs and the select for B1 the B instance with the associated B1 was deleted, throws A.getB().size() an an ObjectNotFoundException. An ObjectNotFoundException during access to a set ist not very intuitive.
There is a possibility to avoid this by using the primary key instead of the business key from B1 in B.hashCode(). But this means that you can only add persistent B1 to B which makes POJO unit tests impossible :-(
Bye
Kai
|