How to avoid a transient property to get executed from a query?

I’m struggle with something can’t find a solution. I have an entity with a transient who retrieves data from a bean. Problem I face, is this is executed and returned always, even if I don’t have this properties enabled on the view I’m retrieving.

My Offer entity has a transient property called lastOffer. That property returns the last OfferDetail object of that Offer. calling a Bean on getLastOffer(), and it works.

When I load that Offer, Offer object returned includes lastOffer even if I didn’t check it on view. Problem is that Object OfferDetail returned, links too to another entity, Person, who has others transients too, then, my object returned is doing a lot of unneeded querys and returning data I don’t want in that moment (it’s a REST call).

I’m doing something wrong? Is not possible to avoid a transient method unchecking on the view?

Regards.

Could you provide the definition of your transient attribute in the Offer entity? Also, where the population of this attribute is triggered?

I’m out right now, but it’s a simple new attribute marked as Transient, and population is done in getter

image

image

 @MetaProperty
public OfertasAnunciosDetalle getUltimaOferta() {
    OfertaSupportService sp = AppBeans.get(OfertaSupportService.class);

    return sp.ultimaOferta(this.getId());
}

@Override
public OfertasAnunciosDetalle ultimaOferta(UUID oferta) {

    DataManager dm = AppBeans.get(DataManager.class);
    LoadContext<OfertasAnunciosDetalle> loadContext = LoadContext.create(OfertasAnunciosDetalle.class)
            .setQuery(LoadContext.createQuery("Select e from project_OfertasAnunciosDetalle e WHERE e.cabecera.id = :cabecera order by e.ordenOferta desc").setMaxResults(1)
                    .setParameter("cabecera", oferta))
            .setView("ultimaOferta-view");

    OfertasAnunciosDetalle lineaOferta = dm.load(loadContext);
    return lineaOferta;
}

So it runs every time as you access the attribute in your code or visual components, regardless of the view.

I would recommend making a transient field for the attribute and populating it via setter in a BeforeDetachEntityListener or in a load delegate, so the loading process will happen in predictable points of your code.
In the latter case you will also be able to skip loading if the view doesn’t have this attribute.

So it runs every time as you access the attribute in your code or visual components,

The thing is I’m not accessing the attribute directly, for this my confusion. I do a query, and retrieve an object using a view. then I return this object as a result of a REST request, and this attribute is included in the JSON even if I don’t have it selected in the view.

Will take a look at load delegate to see how it works. Thanks.

Load delegate is a UI thing, it won’t work if you return the entity through REST.
To find out where the getter is called, temporarily add StackTrace.toString() to it and log its output.