There’s something I’m not quite getting when it comes to fetching attributes in my view. I have an edit screen that works perfectly when it comes to populating the UI components and performing CRUD operations, but it breaks when adding entities in the controller code. Let me give you the background:
I have a “OperationParameter” entity that has a one-to-one composition with a “Parameter” entity. In turn the “Parameter” entity has a one-to-many composition with “ParameterValue” entities. Each “ParameterValue” has a one-to-one association with a “ParameterItem” entity, and lastly the “ParameterItem” has a one-to-many composition with “ParameterList” entities.
Phew, sounds complicated to explain but at the end of the day it’s just a hierarchy. My OperationParameter edit screen has the following data section containing the in-line view:
<data>
<instance id="operationParameterDc"
class="com.gektron.pp.entity.route.OperationParameter">
<view extends="_local">
<property name="parameter" view="_local">
<property name="type" view="_minimal"/>
<property name="unit" view="_minimal"/>
<property name="values" view="_local">
<property name="item" view="_local">
<property name="list" view="_local"/>
</property>
</property>
</property>
</view>
<loader/>
<instance id="parameterDc" property="parameter">
<collection id="parameterValuesDc" property="values"/>
</instance>
</instance>
</data>
As you can see, I’ve made sure that the entire hierarchy is traversed as I do need to fetch data from each level all the way down to the ParameterList. For example, starting at the bottom, the property name “list” refers to the attribute in the ParameterItem, which in turn is referred to by the “item” attribute in the PropertyValue, etc.
As I mentioned this works perfectly for all UI logic and the built-in CRUD logic, nice one CUBA!
However, things start to go wrong when I try to add some ParameterValue entities in my code. I have a function called addValues() shown below:
private void addValues(ParameterType parameterType) {
parameterValuesDc.getItems().forEach(i -> dataContext.remove(i));
parameterValuesDc.getMutableItems().clear();
Parameter parameter = parameterDc.getItem();
if (parameterType != null) {
parameterType.getItems().stream().map(ParameterLink::getItem)
.sorted(Comparator.comparing(ParameterItem::getSort))
.forEach(i -> {
ParameterValue parameterValue = metadata.create(ParameterValue.class);
parameterValue.setItem(i);
parameterValue.setNumber(0.0);
parameterValue.setText("");
parameterValue.setParameter(parameter);
parameterValuesDc.getMutableItems().add(parameterValue);
//i.getList();
});
}
}
In a nutshell, this function deletes the ParameterValue entities currently associated with the Parameter, and then adds new ParameterValue entities based on the ParameterType passed to the function. This code executes without error and updates the UI correctly, all seems good so far, this developer deserves a pat on the back, but wait…
I need to write another part of this function to dig down a bit further into the ParameterItem and ParameterList entities. If I uncomment the last line containing “i.getList()” I get the following error:
IllegalStateException: Cannot get unfetched attribute [list] from detached object com.gektron.pp.entity.parameter.ParameterItem-036cb281-8c4d-d218-698a-45c3ea9ef1af [detached].
I know this is probably simple but I have spent 2 days pouring over the documentation and trying to find any similar scenarios but to no avail. The only clue I’ve got seems to be that different ParameterType entities will have items with different lists (ParameterList), which when referenced by the ParameterValue item will cause the above error because they possibly weren’t fetched when the view was first created.
I have tried various methods of reloading data but nothing works, I continue to get this error. Please help me CUBA, you’re my only hope.