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.
@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:
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 ?