Dynamic attributes

Hello!

How to add dynamic attributes in code. I found many examples of adding through the administration in the application window, but this is not what I need.

Thanks,
Egor

Hi,
Can you specify in more detail, what do you need?

Assign values of the dynamic attributes to the entity instances?
Or add additional dynamic attributes to the entity class?

Hello!

I have an XML file as input to the program, and using it I want to create an entity, where the tags are the attributes of the entity. I don’t know in advance what files will come to the program, so I decided to use dynamic attributes. The question is how to programmatically add dynamic attributes to an empty entity.

Thanks for the answer,
Egor

There is an example in the documentation:

https://doc.cuba-platform.com/manual-7.2/dynamic_attributes.html

Double capacity = entity.getValue("+loadCapacity");
entity.setValue("+loadCapacity", capacity + 10);

Dynamic attribute values are available through getValue() / setValue() methods for any persistent entity inherited from BaseGenericIdEntity . An attribute code with the + prefix should be passed to these methods:

Do I need to some configure the entity before this?
Exception is thrown when processing this line:
Car entity = dataManager.load(Car.class).id(carId).dynamicAttributes(true).one;

Exception: illegalStateException: No results

Thanks for the answer,
Egor

“.one()” method throws this exception if there is no entity with this ID in the database.

Hello!

Thanks, I did it.
Now there is a problem in displaying dynamic attributes on the screen. When I add them to an entity, nothing changes in the entity browser.

Thanks for the answer,
Egor

Hi,
Table components don’t work that way. They don’t automatically display all entity attributes, even static ones. You have to configure something for attributes to appear in the browser.

Screens and components where dynamic attributes are displayed can be configured in the running application, in the dynamic attribute category editor. See documentation: Managing Dynamic Attributes - CUBA Platform. Developer’s Manual

Hello!

I need to configure screens and components where dynamic attributes are displayed not in the running application, but directly in the program code.

Thanks for the answer,
Egor

Well, if you need to configure categories in the program code - you can create all corresponding entities manually in the program.
There are no code samples for that. You can investigate source code of the platform (and data model in the database) to understand how to fill entity attributes for that.
See com.haulmont.cuba.gui.app.core.categories.AttributeEditor and com.haulmont.cuba.core.entity.CategoryAttribute for the start of investigation.

However maybe the dynamic attributes feature doesn’t suit your purpose. If you need completely dynamic attributes without configuring categories beforehand - you can implement them another way.

You can store dynamic attributes in single JSON attribute of the entity. Or you could create a one separate “entity attribute - value” table (The EAV data model: when to use it).
Attributes can be dynamically displayed in the entity browser by using the “generated column” feature of the Table and DataGrid UI components.