LazyInit error on persting with detached association

Description

Basically, the scenario is, if you try to persist an object that has a detached association AND the target of that association has been loaded by some other code for the same session.

When the above conditions are met, Hibernate will throw a LazyInitException during th persist() operation - which if you think about, doesn't really make much sense.

It is not feasible to reset the association to point to the attached version of the object, as the two pieces of code are completely orthogonal, they just happen to refer to the same object within the scope of a single session. This kind of scenario will occur on any sufficiently complex project.

More details can be found here in this forum post: http://forum.hibernate.org/viewtopic.php?t=995977

I have attached some sample code to reproduce the error in as simple a manner as possible: sample.zip

I have also attached a patch with the change suggested by "nordborg" on the forum: patch.txt

Environment

Reproduced on 3.2.5 and 3.3.1 on Ingres and Postgress databases.

Assignee

Vlad Mihalcea

Reporter

Shorn Tolley

Fix versions

Labels

None

backPortable

None

Suitable for new contributors

None

Requires Release Note

None

backportDecision

None

Components

Affects versions

Priority

Major
Configure