This discussion is archived
4 Replies Latest reply: Oct 12, 2012 4:40 AM by user9954330 RSS

INSERTOBJECTQUERY IN POSTINSERT IS NOT BATCHED

user9954330 Newbie
Currently Being Moderated
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?

Thanks,
  • 1. Re: INSERTOBJECTQUERY IN POSTINSERT IS NOT BATCHED
    cdelahun Pro
    Currently Being Moderated
    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.

    Best Regards,
    Chris
  • 2. Re: INSERTOBJECTQUERY IN POSTINSERT IS NOT BATCHED
    user9954330 Newbie
    Currently Being Moderated
    Chris,
    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

    EntityA
    EntityAVersion
    EntityB

    The relationship between EntityA and EntityAVersion share a 1:M relationship with EntityA being the owner
    @OneToMany(cascade={CascadeType.MERGE,CascadeType.PERSIST,CascadeType.REMOVE}, fetch=FetchType.LAZY,
    targetEntity=oracle.communications.platform.entity.impl.EntityAVersionDAO.class, mappedBy="masterVersion")
    @PrivateOwned
    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
    EntityAVersion.setMasterVersion(this);
    em.persist(EntityAVersion)
    em.persist(EntityB);

    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.

    Thanks,
    Rama
  • 3. Re: INSERTOBJECTQUERY IN POSTINSERT IS NOT BATCHED
    cdelahun Pro
    Currently Being Moderated
    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?

    Best Regards,
    Chris
  • 4. Re: INSERTOBJECTQUERY IN POSTINSERT IS NOT BATCHED
    user9954330 Newbie
    Currently Being Moderated
    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
    descriptor.addConstraintDependency(EntityA.class)

    This resolved the issue. All inserts are now batched.

    Thanks for you help.

    Thanks,
    Rama

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points