Generate Model: how to generate UUID value by SELECT query from procedure

I have an Additional data store with DB schema management disabled.
Table ID attribute has VARCHAR type. Its value must be obtained by query from procedure:

select UUID from GET_UUID

What is best way to it ?
I don’t know apropriate JPA annotation.
Entity lifecycle callbacks don’t allow to inject service.
Entity Before Persist event listener ? Please provide example.
Or in editor view controller ? I done, it works.

Little clarification.
I create Service with method:

    public String createUuid() {
        String result;
        try (Transaction tx = persistence.createTransaction(ADDITIONAL_DATASTORE_NAME)) {
            EntityManager em = persistence.getEntityManager(ADDITIONAL_DATASTORE_NAME);
            Query query = em.createNativeQuery("select UUID from GET_UUID");
            result = (String) query.getFirstResult();
            tx.commit();
        }
        return result;
    }

Then in entity edit controller add handler:

    @Subscribe
    public void onInitEntity(InitEntityEvent<Unit> event) {
        if (event.getEntity().getId() == null) {
            event.getEntity().setId(adsService.createUuid());
        }
    }

All works fine ! New entities gets generated uuid.

But when I delete this handler but create entity changed listener as it described in manual EntityPersistingEvent - CUBA Platform. Developer’s Manual

    @EventListener
    public void beforePersist(EntityPersistingEvent<Unit> event) {
        if (event.getEntity().getId() == null) {
            event.getEntity().setId(adsService.createUuid());
        }
    }

It doesn’t work. After “Ok” button pressed in Create new entity dialog list of entities got empty and browser continuously sends POST requests until I close entity browser tab.
But new entity with generated uuid saved.

What wrong ? Why entity browser got empty and POSTs sends infinitely (until entity browser tab close).

I can manage transaction by spring and remove try block:

    @Transactional
    public String createUuid() {
        EntityManager em = persistence.getEntityManager(ADDITIONAL_DATASTORE_NAME);
        Query query = em.createNativeQuery("select UUID from GET_UUID");
        return (String) query.getFirstResult();
    }

CUBA documentation says ‘At the moment of sending the event, an active transaction exists’ so I can even remove @Transactional.
The result remains the same.

Hi,
Maybe the optimal solution for you would be to create the PostConstruct callback in the entity,
and obtain service instance there by using AppBeans.get() method:

@PostConstruct
public void postConstruct() {
    AdsService adsService = AppBeans.get(AdsService.NAME);
    setId(adsService.createUuid());
}

Thus any screen that creates entity instance will know entity ID from the very beginning.

Thanx. This is exactly what I need.
My goal was to set ID closer to entity instantiation.
But why usage beforePersist give so strange result: entity get ID but entity browser become empty and POST requests infinitely sends to server ?
Where is my mistake ?

Have you looked into server logs? Aren’t there any errors in the log?