Well, I really try to get answers for some edge cases from the internet so that I can understand what’s going on and why, but unfortunately whether Google nor SO seem being able to help me here.
I want to understand,
- Why can I use an uninitialized proxy entity, which I obtained via
JpaRepository#getReferenceByIdto set a foreign key for
@ManyToOnewithout having to request the proxy within an upright transaction? (Only the final save will be done in a transaction.)
- Why can’t I go the same way when I wanna set a
@MapsId? In this case I have to obtain the proxy within an upright transaction, otherwise I get an
org.hibernate.PersistentObjectException: uninitialized proxy passed to persist()error.
Aren’t both actions just setting the id? In the end, Hibernate just creates native SQL and could rely on the user, that the provided proxy ids are correct. And regarding point 2, Hibernate doesn’t make an extra call to the db, so why is an initialized proxy required here and not in case 1?
Many thanks for any clarification.