How to pass multi values from an association field to multi fields?

Hi,
I’m a newbie.Thanks for your support. I’d like to know how to pass multi values from an association field to multi fields? For example, a “customer” in an “order” entity is associated with a “customer” entity. If users click […] button and choose a customer, the phone number,address values are pass to phone number and address field in “order” entity. How can I do it?

Hi,

Do you mean, “phoneNumber” and “address” are the attributes of the Order entity, too? Duplicating these attributes is redundant. If you want just to display all attributes of the Customer entity in the order details, include all these attributes in the customer’s View.
The mechanism of views enables loading the related entities to the desired depth. For example, this view is used in the Order editor to display the Product’s name and price.
If you still have questions, don’t hesitate.

You can also take a look at the article which explains the notion of CUBA Views in a simple and figurative way.

Hi,
Yes. I want to duplicate “phone” and “address” from customer entity to order entity.

If you want “address” and “phone” as read-only and non-persistent attributes of the Order entity, simply create getPhone() and getAddress methods in the Order class and annotate them with @MetaProperty:

    @MetaProperty
    public String getAddress() {
        return customer.getAddress();
    }

Another option is to initialize passed values using the @PostConstruct annotation:

    @MetaProperty
    protected String phone;

    @PostConstruct
    protected void init() {
        setPhone(customer.getPhone());
    }

    public void setPhone(String phone) {
        this.phone= phone;
    }

    public String getPhone() {
        return customer.getPhone();
    }

Hi,
Thank you. But I need phone and address persistent in database order table. I need these two fields can be modified as well.

To make these attributes persistent, use @Column instead of @MetaProperty.
For example:

Order.java

    @Column(name = "ADDRESS")
    protected String address;

    public void setAddress(String address) {
        this.address = address;
    }

    public String getAddress() {
        return address;
    }

    @PostConstruct
    protected void init() {
        setAddress(customer.getAddress());
    }

To save changes in the address field both to the Order and to the Customer entities, commit the Customer instance in the Order editor:

    @Override
    protected boolean postCommit(boolean committed, boolean close) {
        if (committed) {
            Customer customer = getItem().getCustomer();
            customer.setAddress(getItem().getAddress());
            customer.setEmail(getItem().getEmail());
            dataManager.commit(customer);
        }
        return super.postCommit(committed, close);
    }

Hi,
Thank you. Your example need users to click the OK button to duplicate the phone and address . How can I duplicate the value to the field on the screen without clicking OK button? I guess the datasource listener can do this but I’m not familiar with it.

You can use datasource listeners or a ValueChangeListener for certain fields to update whatever you want on UI, but you should take care of committing modified instances to avoid transaction issues.

Thank you. I found the solution from other threads.

1 Like