After setting the value of the transient field in the browse screen, click Edit to find that the value is lost in the edit screen. What should I do?
You should save it somehow. Usually, calculated fields are set to transient ones. What are you going to achieve by using the transient field type?
Sorry, I may not describe it clearly. It’s a non persistent field on entity.
@Transient
@MetaProperty
protected Integer status=0;
Well, it makes sense actually. It is a transient field so it is not saved.
When opening the editor screen, cuba will go to the db to reload the entity. This is desired behaviour, someone else might have changed the entity in the mean time and you want the latest version.
have a look at EditAction - CUBA Platform. Developer’s Manual
I think the screenOptionsSupplier or screenConfigurer should help you out.
Sorry, I just cannot get it. What are you going to achieve? It is an entity status, why don’t you save it? If it is derived from an entity’s attributes, why don’t you make it a calculated attribute?
Because this value will be obtained from the third-party interface and not in the database, but it needs to be displayed in the browser screen. I found that the value exists when the edit screen is opened, but it is lost because the entity is merged after the DC is loaded. I think it’s more reasonable for the @ transient field to keep its previous value when merging.It is impossible for other users to modify this value at the same time through the database.
Then you can use getter instead of the class field and implement proper logic in it. Something like this
@Transient
@MetaProperty
protected Integer getStatus() {
return canBeFetched() ? obtainValue() : 0;
}
Well, I get the status of entities in bulk through the interface. If you get one by one, the efficiency will be very low.
Fair enough.
How do you assign them to entities then? Could you provide a bit of code please?
It seems that I can only save this value in the edit screen and write it back after the data is loaded. However, I still think the system should not merge this value by default. Please consider this suggestion
I set this value in bulk in the onDlPostLoad event of the browser screen
Then it won’t be saved. You can pass the value as a screen parameter to the editor by overriding the action as described in the documentation.
I don’t need to pass it. As I said above, the edit screen can get this value, but the value is set to null when merging data. What I want to say is that this value should not be set during data merging.
Ah, sorry, got it now.
In order to consider your suggestion, we’d like to get a minimal example of your code that would repeat the issue and a detailed description of the desired behavior. I think I understand you now, but a line of code worth thousands of words
This is how I deal with it now. The code in edit screen is as follows:
@Subscribe(id = "routerLc", target = Target.DATA_LOADER)
public void onRouterLcPreLoad(InstanceLoader.PreLoadEvent event) {
routerStatus=this.getEditedEntity().getStatus();
}
@Subscribe(id = "routerLc", target = Target.DATA_LOADER)
public void onRouterLcPostLoad(InstanceLoader.PostLoadEvent event) {
this.getEditedEntity().setStatus(routerStatus);
dataContext.commit();
}
But it looks ugly!
This is how I set the value in the browse screen
@Subscribe(id = "routersDl", target = Target.DATA_LOADER)
public void onRoutersDlPostLoad(CollectionLoader.PostLoadEvent<Router> event) {
synchronized (timer) {
List<Router> routers = event.getLoadedEntities();
routersDl.getContainer().getMutableItems().clear();
routersDl.getContainer().getMutableItems().addAll(routerService.checkStatus(routers.toArray(new Router[routers.size()])));
}
}
Everything else is the default operation, nothing special