We have implemented custom logic in the postInsert life cycle event of Entity A. In this event we are inserting a new Entity B in to database using InsertObjectQuery. We have enabled batch writinig. We are observing the entity A (which is persisted using em.persist call) is inserted in batches. But EntityB, for each single insert new batch is created. Is it because we have used InsertObjectQuery or is it because of lifecycle implementation? How do we batch these inserts as well?
By issuing a WriteQuery directly, the changes are not arranged and ordered with the changes calculated by the UnitOfWork.
I'd recommend moving it to the application logic rather than an event if possible. If not, you can use the prePersist event which is fired when the A object is persisted and then persist or register the B entity, so that B is included in the UnitOfWork's list of managed objects and the insert ordered accordingly.
Thanks for your suggestion. It is not possible right now to move this logic to application from events. So I followed your suggestion on using prePersist. It worked partially.
Here the actual entity model
The relationship between EntityA and EntityAVersion share a 1:M relationship with EntityA being the owner
protected java.util.Set<oracle.communications.inventory.api.entity.EntityAVersion> versions;
and EntityA,EntityAVersion go to same physical table. When EntityA gets stored, the INSERT statemnt will not have masterVersion but when EntityAVersion gets inserted it has this column.
EntityB is a independent entity sharing no relationship.
In the prePersist implementation of EntityA,
We are calling
If I try to create 3 instances of EntityA, I see that three EntityB inserts are batched. But the EntityA and EntityAVersion are not batched still. INSERT statements are executed in multiple batches i.e one for each batch.
Please let me know if any other information is required.
Because the EntityA ->EntityAVersion relationship is marked cascade persist, you shouldn't need to manually call em.persist(EntityAVersion) in the prePersist method. Does this problem occur if you do not have a prePersist method (and so are not persisting EntityB)? Can you try removing the @PrivateOwned on the EntityA ->EntityAVersion relationship?
I tried commenting the persist call on EntityAVersion and setting the relationship before calling persist on EntityA. This only partially solved the issue. EntityAVersion and EntityB are getting inserted in batches. However EntityA records are still getting inserted in individual batches.
Based on the finding the the calculated commit order has EntityAVersion first before EntityA , I have tried your other suggestion of calling the following on EntityAVersion descriptor
This resolved the issue. All inserts are now batched.
Thanks for you help.