Jmix Data Import Association attribute

Hello to everyone

I´ve got a functionality question about the Data Import module.
Starting from this documentation point:

ImportConfiguration importConfiguration = ImportConfiguration.builder(Order.class, InputDataFormat.XLSX)
                .addSimplePropertyMapping("number", "Order Number")
                .addSimplePropertyMapping("amount", "Order Amount")
                .addPropertyMapping(ReferenceMultiFieldPropertyMapping.builder("customer", ReferenceImportPolicy.CREATE_IF_MISSING)
                        .addSimplePropertyMapping("firstName", "Customer First Name")
                        .addSimplePropertyMapping("lastName", "Customer Last Name")
                        .addSimplePropertyMapping("email", "Customer Email")
                        .withLookupPropertyNames("firstName", "lastName")
                        .build())
                .withPreImportPredicate(extractionResult -> {
                    Order order = (Order) extractionResult.getEntity();
                    if (order.getNumber() == null) {
                        return false;
                    } else {
                        order.setDate(currentDate);
                        return true;
                    }
                })
                .withTransactionStrategy(ImportTransactionStrategy.TRANSACTION_PER_ENTITY)
                .build();

We have .withPreImportPredicate which help us to avoid duplication and also to set some attributes that are not in the imported document (XLSX in my case).
So far so good but can we set a Many-to-One attribute this way? Cause` if I try to do this… :

Order order = (Order) extractionResult.getEntity();
Customer customer = dataManager.load(Customer.class).all().one();
order.setDate(currentDate);
order.setCustomer(customer);

…only the date attribute is saved in the database.

P.D… The PropertyMapping “association way” works perffectly.

using: LATEST JMIX VERSION AND LATEST INTELLIJ VERSION TOO

Thank you in advance

Good morning

After a repairing sleep I found out that if you write it this way:

.addCustomPropertyMapping("customer", e -> customer)

It works. But I still don´t know why the other way is not valid.
I will mak this as a solution but if someone is kind enough to explain me…

Thank you

Hi, @mihai.maranduca.

Reference properties are included in the entity import plan only if a declared property mapping is specified in the import configuration.
You can also use a custom property mapping to include a reference property in the entity import plan.
For example:

 ImportConfiguration importConfiguration = ImportConfiguration.builder(Order.class, InputDataFormat.XLSX)
                .addSimplePropertyMapping("number", "Order Number")
                .addSimplePropertyMapping("amount", "Order Amount")
                .addCustomPropertyMapping("customer", customMappingContext -> {
                    Customer customer = dataManager.load(Customer.class).all().optional()
                            .orElse(null);

                    return customer;
                }) 
                .build();

Regards,
Maria.

Hi Maria

Thank you very much for your solution.

Kind regards