Hi,
I should like have an help for this problem about hibernate lazy collection mapping. My question is conceptual more than practical and is intended to understand the real way hibernate works and perhaps is not trivial.
I think. I read many times the hibernate documentation but was not able to find or build an aswer for my problem.
Sorry for the length of the post, most of it is an explanation to better explain the real final question.
I need to manage a parent/child relationship where an array is provided in the parent class to bind child class istances. The association is bidirectional. This tree of istances should be handled by concurrent web clients each one “catching” istances of both parent and child classes in an exclusive way (x-lock on db). Add or remove of child istances can be done only through parent’s methods. Clients can instead directly access child istances for inner istance modifications. Contention on an istance, parent or child, will be managed by handling xlock timeout and refuse clients the access to an already locked istance.
The point is that when adding/removing child istances the parent should be able to scan the binding array to verify “not null” references (a child is bound in this slot/ no child is bound in this slot) and to invoke the getId method on the bound istances (the child at index I has this id) and all that absolutely without hitting the child table on the DB.
In fact I need the parent istance to detect/verify single or multiple unbound “slots” in the array and verify the ids of one or more (already) bound children as a pre-condition to perform the requested operations (like adding or removing childs). Since some or all bound child istances could be concurrently x-locked by their managing client, the parent cannot do the work by hitting the child istances on the db. The contention on an istance is to be managed only after the said verifications and consequent decision of performing an operation on a child istance (deletion / modification).
To resume, what I need is that at the first load in session of a parent istance hibernate build proxies for all the db-persistent bound child istances, build an indexed hibernate collection for the array, and bind all them at java identity level, without hitting the child class table on the db.
This is, I believe, impossible if the parent-child relationship is mapped via foreign key / index key on the child class table. In fact hibernate should hit all the child istances rows to retrieve ids and indexes, and this should cause the unwanted collision on already x-locked rows.
But, perhaps, if the bidirectional parent-child relationship is mapped through an intermediate collection table, when loading the parent istance hibernate could retrieve from the db the related collection table section (only). The collection table contains all the needed information to build the hibernate collection and all the child istances proxies. No problem for child side reference to the parent istance being "locked" in the collection table associated to the parent itself, since child istances binding/unbinding can be performed by the parent side only, and after locking the child (when no contention happens). No hit on the child istances db rows, no collision, possibility to scan the array for java references and to invoke getId for all the child istances.
Now, if what I’m saying makes sense, my question is:
- Can I obtain this by mapping the relationship as bidirectional one-to-many/many-to-one with intermediate collection table + setting “extra lazy fetching” for the indexed collection (array) + setting lazy fetching + setting automatic cascading on load for child istances?
- Or do it exists another way to make do this by hibernate ? And how?
Thank you for your patie
|