Unfetched attribute error on metaproperty, although meta property is not included on the screen

Hello,
I made a minimum test project to show the case. Just create a new entity1 (with entity2 and entity3) and after successful creation try to edit the created entity1.

metapropertyunfetched.zip (102.8 KB)

It will fail with the following stacktrace, despite the attribute not being used in the edit screen:

Caused by: java.lang.IllegalStateException: Cannot get unfetched attribute [entity3] from detached object com.company.metapropertyunfetched.entity.NewEntity2-bca13344-745d-1bb1-3655-eec5bcded1ef [detached].
	at org.eclipse.persistence.internal.queries.EntityFetchGroup.onUnfetchedAttribute(EntityFetchGroup.java:100) ~[org.eclipse.persistence.core-2.7.3-14-cuba.jar:na]
	at com.haulmont.cuba.core.sys.persistence.CubaEntityFetchGroup.onUnfetchedAttribute(CubaEntityFetchGroup.java:74) ~[cuba-global-7.2.13.jar:7.2.13]
	at org.eclipse.persistence.internal.jpa.EntityManagerImpl.processUnfetchedAttribute(EntityManagerImpl.java:2998) ~[org.eclipse.persistence.jpa-2.7.3-14-cuba.jar:na]
	at com.haulmont.chile.core.model.impl.AbstractInstance._persistence_checkFetched(AbstractInstance.java) ~[cuba-global-7.2.13.jar:7.2.13]
	at com.company.metapropertyunfetched.entity.NewEntity2._persistence_get_entity3(NewEntity2.java) ~[app-global-0.1-SNAPSHOT.jar:na]
	at com.company.metapropertyunfetched.entity.NewEntity2.getEntity3(NewEntity2.java:17) ~[app-global-0.1-SNAPSHOT.jar:na]
	at com.company.metapropertyunfetched.entity.NewEntity1.getEntity3Id(NewEntity1.java:20) ~[app-global-0.1-SNAPSHOT.jar:na]
	at com.haulmont.chile.core.model.impl.AbstractInstance.getValue(AbstractInstance.java:108) ~[cuba-global-7.2.13.jar:7.2.13]
	at com.haulmont.cuba.core.entity.BaseGenericIdEntity.getValue(BaseGenericIdEntity.java:146) ~[cuba-global-7.2.13.jar:7.2.13]
	at com.haulmont.cuba.gui.model.impl.DataContextImpl.mergeState(DataContextImpl.java:267) ~[cuba-gui-7.2.13.jar:7.2.13]
	at com.haulmont.cuba.gui.model.impl.DataContextImpl.internalMerge(DataContextImpl.java:217) ~[cuba-gui-7.2.13.jar:7.2.13]
	at com.haulmont.cuba.gui.model.impl.DataContextImpl.merge(DataContextImpl.java:162) ~[cuba-gui-7.2.13.jar:7.2.13]
	at com.haulmont.cuba.gui.model.impl.InstanceLoaderImpl.load(InstanceLoaderImpl.java:112) ~[cuba-gui-7.2.13.jar:7.2.13]
	at com.haulmont.cuba.gui.model.impl.ScreenDataImpl.loadAll(ScreenDataImpl.java:80) ~[cuba-gui-7.2.13.jar:7.2.13]
	at com.haulmont.cuba.gui.model.impl.AnnotationBasedLoaderBeforeShow.loadData(AnnotationBasedLoaderBeforeShow.java:36) ~[cuba-gui-7.2.13.jar:7.2.13]
	at com.haulmont.cuba.web.sys.WebScreens.loadDataBeforeShow(WebScreens.java:555) ~[cuba-web-7.2.13.jar:7.2.13]
	at com.haulmont.cuba.web.sys.WebScreens.show(WebScreens.java:447) ~[cuba-web-7.2.13.jar:7.2.13]
	at com.haulmont.cuba.gui.screen.Screen.show(Screen.java:309) ~[cuba-gui-7.2.13.jar:7.2.13]
	at com.haulmont.cuba.gui.actions.list.EditAction.execute(EditAction.java:351) ~[cuba-gui-7.2.13.jar:7.2.13]
	at com.haulmont.cuba.gui.actions.list.EditAction.actionPerform(EditAction.java:298) ~[cuba-gui-7.2.13.jar:7.2.13]
	at com.haulmont.cuba.web.gui.components.WebAbstractTable.handleClickAction(WebAbstractTable.java:1295) ~[cuba-web-7.2.13.jar:7.2.13]
	at com.haulmont.cuba.web.gui.components.WebAbstractTable.lambda$initComponent$db766529$1(WebAbstractTable.java:1048) ~[cuba-web-7.2.13.jar:7.2.13]
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
	at java.base/java.lang.reflect.Method.invoke(Method.java:566) ~[na:na]
	at com.vaadin.event.ListenerMethod.receiveEvent(ListenerMethod.java:496) ~[vaadin-server-8.9.2-19-cuba.jar:8.9.2-19-cuba]
	... 45 common frames omitted

Hi,

You can use one of methods: EntityStates.isLoaded() or isLoadedWithView() inside of your MetaProperty method to avoid unfetched attribute exception when corresponding property is not loaded.

Take a look at similar topic:

Hi @albudarov, thank you, that works. It feels like a workaound though and IMHO shouldn’t be necessary in the case I described.