Creating an entity that consists of its instances

I know that the relational data model doesn’t allow us to do something like that but I have to find a way out.

Let’s say I try to create a spare part shop, and can sell parts themselves and assembled ones. I want to create an entity SparePart (let’s say ‘engine’) that contains a list of other SparePart’s. No problem with data model, I create an entity SparePartsList and everything goes fine. The problem is in creating views and screens for editors. Obviously.

I’m stuck on creating a list. I don’t want to create a separate ListEditor screen, I want to be able to see the list of inner parts (if list is not null) and to add more parts to the list right from the view SparePartEditor.

I guess my question is so complex that it needed a full guide, that’s why I’m sorry for asking it in that way.

The problem is not so obvious, at least for me. For example, in your SparePart entity you can make a reference to itself and show it in a Tree or TreeTable. Can you explain in more detail what do you want to achive, better with UI sketches?

I create an entity SparePart. But how to “say” that it can contain a ListOfSpareParts? If I create an association attribute “ListOfParts” of type SparePart with cardinality one_to_many it requires a “Mapped by” field which I can’t fill cos I don’t have any. M_2_m fires an error in DB creation.

So create this inverse ManyToOne attribute, say “masterPart”. See the diagram attached.

2017-06-27 16_29_07-unnamed0 - yEd

I can’t do it via Studio. The list of ‘mapped by’ options is empty, even when I manually created a mapping attribute

If I create a list of entities as a separate entity in a data model than I have a problem working with views. And getting needed values to the needed fields. See the included file (sorry for handwriting)

image

And as I create an entity with code, I can’t see it on a screen even if I add it to a view…
Sorry for that lot of comments, but I’m trying options, and they don’t work properly… yet

In order to create an association with self, first create and save the entity without this association. Then edit the entity again and you will be able to select the same entity in the Type dropdown.

If you still have problems with displaying your data structure, please create a small test project with your data model and screens (even if they are not working as expected) and explain in details what do you need with relation to this test project.

I have it in a Type Dropdown but I haven’t a ‘mapped by’ attribute in a dropdown. Anyway I will do the description for a sample project for this issue

Yes, first create the “parent” attribute which is a ManyToOne association. Then you will be able to use it as a mappedBy attribute when creating OneToMany collection.

Thanks for helping me creating the folded item. But i still can’t understand how to make inner entities visible on browse-edit screens. Can you, please point me to the place in user-manual, where i can find information about it. I attach my progress about the issue so far… Thanks in advance.

inner-entity.zip (19.3K)

You can easily display hierarchical structures in a TreeTable - see the project attached.
In order to set the parent entity to the currently selected item, add the following to the browser screen:

public class SparePartBrowse extends AbstractLookup {

    @Named("sparePartsTable.create")
    private CreateAction sparePartsTableCreate;

    @Inject
    private TreeTable<SparePart> sparePartsTable;

    @Override
    public void init(Map<String, Object> params) {
        sparePartsTableCreate.setInitialValuesSupplier(() -> {
            SparePart selected = sparePartsTable.getSingleSelected();
            return selected != null ? ParamsMap.of("sparePartMapping", selected) : null;
        });
    }
}

is there a way to download that live demo? (i have no internet connection available at my workplace but it would be nice to examine it)

I think you will be able to build the project if you change the repository settings in build.gradle back to yours.

This is a confusing situation. are you sure you attached a fie? or i can’t see how to get a live demo to examine it offline

Sorry, I really missed the attachment. Now it’s here.

inner-entity.zip (28.2K)

1 Like

First of all, thank you for your job. but now as i open an entity editor screen - it throws an exception that IndirectList cannot be cast to Entity. i guess i have to make additional field on an edit screen, that should hold a list, and somehow convert list to string?

Yes, or just remove to-many attributes from the field group and represent them as tables.