Entity listener to update another entity

I have master detail tables (InventoryReceiptsVendorH and InventoryReceiptsVendorD) and InventoryBalance. I have a listener as follows but noticed after debugging that the query is not returning any record to update the 3rd entity.


@Component("mydb_InventoryReceiptVendorHListener")
public class InventoryReceiptVendorHListener implements BeforeInsertEntityListener<InventoryReceiptVendorH>, AfterDeleteEntityListener<InventoryReceiptVendorH>, AfterInsertEntityListener<InventoryReceiptVendorH>, AfterUpdateEntityListener<InventoryReceiptVendorH> {

    @Inject
    private UniqueNumbersService unService;

    @Inject
    private Persistence persistence;

    @Inject
    private Metadata metadata;

    @Override
    public void onBeforeInsert(InventoryReceiptVendorH entity) {
        entity.setDocNumber(unService.getNextNumber("ReceiptVendor_Number"));
        entity.setMovementType("104");
    }

    @Override
    public void onAfterDelete(InventoryReceiptVendorH entity) {

    }

    @Override
    public void onAfterInsert(InventoryReceiptVendorH entity) {
        insertEntries2(entity);
    }


    @Override
    public void onAfterUpdate(InventoryReceiptVendorH entity) {

    }

    private void insertEntries2(InventoryReceiptVendorH operation) {
        EntityManager em = persistence.getEntityManager();

        TypedQuery<InventoryReceiptVendorD> query = em.createQuery("select e from mydb$InventoryReceiptVendorD e " +
                "where e.inventoryReceiptVendorH.id = ?1 ", InventoryReceiptVendorD.class);

        query.setParameter(1, operation.getId());
        List<InventoryReceiptVendorD> lists = query.getResultList();
        for(InventoryReceiptVendorD list : lists) {
            InventoryTranDetail tranDetail = metadata.create(InventoryTranDetail.class);
            tranDetail.setMovementType(operation.getMovementType());
            tranDetail.setMaterial(list.getMaterial());
            tranDetail.setQuantity(list.getQuantity());
            tranDetail.setWarehouse(operation.getWarehouse());
            em.persist(tranDetail);
        }
    }

Thanks for helping where i am making a mistake.

Don’t use EntityManager in After* listeners - this is clearly stated in the documentation and in JavaDocs on listener interfaces.
Your code should work if you move it to BeforeInsert listener.

Thanks.

Just tried but still the same,


 @Override
    public void onBeforeInsert(InventoryReceiptVendorH entity) {
        entity.setDocNumber(unService.getNextNumber("ReceiptVendor_Number"));
        entity.setMovementType("104");
        insertEntries2(entity);
    }

Snip20160822_1

Hi
I have another old post where i have attached a sample application for help. You may close this one and help there.
Mortoza

Sorry, I don’t understand the logic behind your query. You insert a master record, and at this very moment you try to find detail records that reference this master. How do you expect they exist?

Please refer to the following post which is similar and where I need help:

https://www.cuba-platform.com/discuss/t/entitylistener-onafterupdate-working-twice

the detail entity is a composite entity and was expecting it will be updated when the main entity is updated. If that is not the case, how can I use composite entity to update another table ? Any help is appropriated l.

ok, I have resolved it. thanks.