I have the following scenario:
- I have two entity classes: X and Y. X has a @ManyToOne association to Y which is not cascaded.
- I create an (unmanaged) instance x of X and an (unmanaged) instance y of Y, and fill the reference to y in x.
- Entity y already has a corresponding row in the underlying database, but entity x is new.
- I persist entity x.
When I perform this scenario, I expect to see one query: INSERT x. However, what actually happens is that Hibernate performs TWO queries:
Furthermore, I also notice that after the persist of x, the reference to y does not actually become managed and there is no instance of Y in the session! So, why is the SELECT on y performed at all? Is there a way to prevent this?