[1.5x, though probably the same in 2.x] Performance question: reloading entities to ensure attributes are available - is there a better way?

So, in services I’m often working with an Entity that’s passed in. I have no way to know what view/fetch plan it was loaded with, so no real way to know which attributes are available vs. which ones will cause a dreaded IllegalStateException when accessed.

So, defensively I just reload it using a DataManager call in the form of:

foo = dataManager.load(Foo.class).id(foo.getId()).fetchPlan("really-complete-foo-fetch-plan-that-has-all-the-attributes-because-illegalstateexceptions-suck").one();

This works, of course, and voila, no ISE’s, ever. But is this harming performance? Is there a better way? Is there a way to just force things to work more intelligently, and realize that if I want to access an attribute and it’s not loaded…perhaps the system should just load it on demand?

Hi, Jon

In such cases, we use the entityStates bean. It has many methods to check the state of an entity.
You can check if the attribute is loaded entityStates.checkLoaded(entity, "field1") or entityStates.isLoaded(entity, "field1"), or chech current fetch plan and etc.

Then you reboot the instance only if it’s really necessary.

It has many other useful methods.

image

1 Like

It happens that they passed an entity loaded from a fetch plan that does not have 1-2 attributes I need.
Then you can check it, supplement the fetch plan with only the necessary attributes and reload.

if (!entityStates.isLoaded(entity, "field1")) {
            FetchPlan currentFetchPlan = entityStates.getCurrentFetchPlan(entity);
            FetchPlan myfetchPlan = fetchPlans.builder(currentFetchPlan).add("field1").build();
            foo = dataManager.load(Foo.class).id(entity.getId()).fetchPlan(myfetchPlan).one();
        }
1 Like