|
I think it was since Hibernate 4 that this "protection" was created that makes Hibernate throw this exception in various situation on ManyToOne associations. AFAIC this is almost always unexpected and unwanted and just serves to make Hibernate more semantically correct at the expense of productivity and reliability.
In ManyToOne situations normally you wouldn't run into this situation because you're just storing an id. However, another practice: using CascadeType.MERGE to ensure detached entities get automatically get reattached, which I have always thought to be a valid strategy, combines with this to throw this exception in various situations.
Now, I find myself rewriting a lot of code to just load the entity that I want to attach from the database in stead of relying on CasaceType.MERGE.
I would argue that at the very least we should be able to disable this functionality. I would very happily trade the off-chance that some record gets overwritten with "stale information" (that is in all likelihood exactly the same as the original) against fighting against this feature.
|