Auto-increment number for ASSOCIATED entities

Hi

I have the following entity Job which has multiple Container entities associated with it. (Container entities are created in the screen controller). When I save the Job, I want the Containers to receive unique registry numbers

I am using before entity listener like this:

@Override
public void onBeforeInsert(Container entity, EntityManager entityManager) {


    if (entity.getNrRegistru() == null) {
        Query query = entityManager.createNativeQuery(
                "select max(NR_REGISTRU) from llg_container where delete_ts is null");
        Long nrReg = (Long) query.getSingleResult();
        if (nrReg == null) nrReg = new Long(0);
        entity.setNrRegistru(nrReg + 1);
    }

But they all receive the same number.

What do I need to change to make it work?

Thank you

Take a look here at docs

1 Like