Get a value from another Entity

Hello,

What is wrong here?

    @Inject
    private Metadata metadata;

    @Override
    protected void initNewItem(Transaction item) {

       Quote quote =  metadata.create(Quote.class);
       item.setDate(quote.getDate());
   }

Hi,

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?

Hello,

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

Hi,

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.

Hello,

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

 @Override
    public void onAfterUpdate(Quote entity, Connection connection) {




            try{

                EntityManager em = persistence.getEntityManager();

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


                log.debug("DDDDDDDDDDDDDDDDDDDDDATE="+entity.getTransaction().getId());

                List<Date> results = query.getResultList();
                if(results != null && results.size() >0) {
                    Date date = results.get(0);//.getDate();
                    log.debug("DDDDDDDDDDDDDDDDDDDDDATE="+date);
                    //entity.getTransaction().setDate(date);

                    Query query2 = em.createQuery(
                            "update crm$Transaction t set t.date=:dateParam where t.id=:idTransaction");

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

                    query2.executeUpdate();
                }

                tx.commit();
                tx.close();

            } 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:
image
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.

 @Named("quoteTable.edit")
    private EditAction quoteTableEdit;

@Override
    public void init(Map<String, Object> params) {
    quoteTableEdit.setAfterCommitHandler();
}

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 {


    @Named("transactionsTable.edit")
    private EditAction transactionsTableEdit;


    @Inject
    private GroupDatasource<Transaction, UUID> transactionsDs;

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




}