Get a value from another Entity


What is wrong here?

    private Metadata metadata;

    protected void initNewItem(Transaction item) {

       Quote quote =  metadata.create(Quote.class);


If something doesn’t work, please be more specific about what’s broken.

I have a One-To-Many relation between transaction and quote (s).
In transaction I have date attribute.
In quote I have date attribute.

I want to set in transaction(date attribute) data value from quote (date attribute).

What is the method to access attribute value from another entity?


What is the right method in order to access an Entity (value attribute) from another Entity?


According to the above code, you try to set the date value during a new entity creation, but the new entity has no quotes, therefore, you can’t get a date from them. I will be able to help you only if you describe what you want to achieve in more details.


Yes you are right. So I tried another approach (create a Listener) - see below:

    public void onAfterUpdate(Quote entity, Connection connection) {


                EntityManager em = persistence.getEntityManager();

                com.haulmont.cuba.core.Transaction tx = persistence.createTransaction();
                TypedQuery<Date> query = em.createQuery(
                        "SELECT MAX( FROM crm$Quote e where = :idTransaction", Date.class);
                query.setParameter("idTransaction", entity.getTransaction().getId());


                List<Date> results = query.getResultList();
                if(results != null && results.size() >0) {
                    Date date = results.get(0);//.getDate();

                    Query query2 = em.createQuery(
                            "update crm$Transaction t set where");

                    query2.setParameter("dateParam", date);
                    query2.setParameter("idTransaction", entity.getTransaction().getId());



            } catch(Exception e) {e.printStackTrace();}


The problem is when I use AfterUpdateListener I have no modification until I Edit again Transactions (but it works).
On the other hand if I use BeforeUpdateLIstener I have a runtime error:
and in log file: The object Transaction xxx cannot be updated because it has changed or been deleted since it was last read.

My goal is to set (automatically) in transaction date, max (last) date from Quote list (after I modified Quote date).

When a new entity is created or an existing one is updated using CreateAction/EditAction the table datasource doesn’t reload all data, but simply add/replace created/editeed item. In your case, AfterUpdateListener called after an item was added/replaced in table datasource. You can check this by clicking the filter’s search button. As an option, you can inject CreateAction and EditAction and set AfterCommitHandler for them that will refresh the datasource.

and why does’t work entity.getTransaction().setDate(date);? I expected to work :slight_smile:

 As an option, you can inject CreateAction and EditAction and set AfterCommitHandler for them that will refresh the datasource.

Could you be more specific…where should I do that? Can you give me a code snippet, pls?
I suppose that I have to inject in Transaction controller but I don’t know how to set AfterCommitHandler for them.

    private EditAction quoteTableEdit;

    public void init(Map<String, Object> params) {

Take a look at the documentation. It contains an example of AfterCommitHandler usage.

Ok, but refreshing Quotes doesn’t help me, I need to refresh Transactions.
So I should inject EditAction in TransactionBrowse controller.

We will be able to help you if you send us a small sample project along with reproduction scenario that demonstrates the issue.

It works. Thank you very much, Gleb!!:balloon:

public class TransactionBrowse extends AbstractLookup {

    private EditAction transactionsTableEdit;

    private GroupDatasource<Transaction, UUID> transactionsDs;

    public void init(Map<String, Object> params) {
        transactionsTableEdit.setAfterCommitHandler(entity -> transactionsDs.refresh());