uniqueNumbers in Cuba7

Hello,

Can you give me an example how this code is written in Cuba 7?

 @Inject
    private UniqueNumbersAPI uniqueNumbersAPI;

    @Override
    public void onBeforeInsert(Invoice entity, EntityManager entityManager) {
    entity.setRegistrationNumber(String.format("%s%04d", "xyz", uniqueNumbersAPI.getNextNumber("mySeq")));
}

Now in the Beans there is option to choose four phases …using @TransactionalEventListener, is quite different…how could I generate Entity Listeners?

First of all, you can still use entity listeners in CUBA 7+, but Studio doesn’t have option to generate them. See documentation for how to write them manually.

In CUBA 7.1 you can use EntityPersistingEvent to assign attribute values for newly created entities:

@Component("demo_CustomerChangedListener")
public class CustomerChangedListener {

    @Inject
    private UniqueNumbersAPI uniqueNumbersAPI;
    
    @EventListener
    void beforePersist(EntityPersistingEvent<Customer> event) {
        Customer customer = event.getEntity();
        customer.setRegistrationNumber(String.format("%s%04d", "xyz", uniqueNumbersAPI.getNextNumber("mySeq")));
    }
}
1 Like