"Cannot get unfetched attribute from detached object" after initNewItem

I’m using the initNewItem to populate entities with composition in an edit screen controller.
I also have a table with the nested groupDatasource, and I’m using on the owning Entity a view with contains all the needed attributes to populate the table. It works fine on editing an existing Entity, but on Entity creation I get the error in the title.
Basically, it looks like the fetching setup in the view doesn’t work when the new sub-entitites are created with initNewItem. Should I manually fetch the related entities in the initNewItem?

Hi Paolo,

Views are used only in the process of loading entities from the database and they don’t affect instances just created in memory. In a new instance all attributes can be set.

Could you reproduce your problem on a small test project?

1 Like

Hi Konstantin,
just to be sure I’m getting it right before making a test project. I need to fetch several related entities to set a column in the table containing newly created instances. This happens automatically using the view when editing an instance, but how am i supposed to manually fetch the related entities after initNewItem?


If you need some reference entities to set attributes of your new entities, you can use DataManager to load them at any time. Sorry if I still don’t understand your situation.

I’m already setting the attribute, what I need is the table to “traverse” that attribute to fetch another attribute.

Like so:
I tried manually fetching all the intermediate entities with the dataManager, but I get the same error.
BTW I’m preparing a project to demonstrate this

I’m attaching the project. Steps to reporudce:

  • make at least 1 Definition1 instance
  • make some Definition2 instances. Each one has a many-to-one relationship with Definition1
  • try to make a ContainerEntity. It will trigger this code:

    protected void initNewItem(ContainerEntity containerEntity) {
        Set<ContainedEntity> items = new HashSet<>();
        for (Definition2 definition : getAllDefinitions()) {
            ContainedEntity containedEntity = metadata.create(ContainedEntity.class);
            containedEntity.setName("name test");

    private ArrayList<Definition2> getAllDefinitions() {
        LoadContext.Query query = LoadContext.createQuery("select e from compositiontest$Definition2 e");
        return new ArrayList<Definition2>(dataManager.loadList(LoadContext.create(Definition2.class).setQuery(query)));

compositionTest.zip (366.8K)

Hi Paolo,

Just specify a view when loading entity:

    private ArrayList<Definition2> getAllDefinitions() {
        LoadContext.Query query = LoadContext.createQuery("select e from compositiontest$Definition2 e");
        LoadContext<Definition2> loadContext = LoadContext.create(Definition2.class)
                .setView("definition2-view"); // defines the object graph to load
        return new ArrayList<Definition2>(dataManager.loadList(loadContext));

Thank you very much. I was misleaded by trying to set the view on the item instead of the Definition2, to replicate the screen normal behaviour