We are using Hibernate 3.5.6-Final with Hazelcast 3.6.1 2nd level cache.
I have a bi-directional, one-to-many relation between a Parent
There's a better way:
You need to make the many-to-one side lazy.
As long as you don't need to fetch the Parent entity in the currently running Persistence Context, you can just fetch a Proxy reference:
Parent parentProxy = session.load(Parent.class, parentId);
Now, you can simply create a new child as follows:
Child newChild = new Child();
child.setParent(parentProxy);
session.persist(newChild);
Another workaround is as follows:
You don't even fetch a Parent Proxy reference, but instead, you do as follows:
Parent parentReference = new Parent();
parentReference.setId(parentId);
Child newChild = new Child();
child.setParent(parentReference);
session.persist(newChild);
This way, you don't need to fully fetch the Parent entity if all you need is to persist a Child entity.