Fetch Related Attribute Without Including it in View

For a few of my entities, I have a field that is obtained by iterating over a field of an associated entity.

For example, a “balance” field, which iterates over all of a client’s items and sums up the items’ balance.

In some views (I am using Polymer, not the GUI), I want to be able to see the balance, but I don’t need to retrieve the client’s items. But since I have to include the item in the view, they are all downloaded to the client. This is made even worse in my case, since each item further calculates the balance based on an iteration over its nested entity.

I’ve tried using the related() parameter for @MetaProperty, but it doesn’t seem to let it work (cannot get unfetched attribute from detached object).

Is there a way to be able to fetch attributes that won’t be included in the view?

Your question looks a bit paradoxical to me. Do you want to get the balance value without loading the information that is needed to calculate the balance?

Or you just want to specify “balance” in the view and get the needed items loaded? If so, the “related” attribute of @MetaProperty should do exactly this. If it doesn’t, please tell us what platform version you use, and give more details about your data structure and view definition.

I can see how my question would appear paradoxical. Let me try to explain it a bit better.

I would like to calculate “balance”, which requires some fields in nested entities, without then having to also return those fields in the nested entities to the REST request.
So, if “entity.balance” used “entity.listOfSubEntities[].balance” to be calculated, in the resulting view I would like to download via REST “entity.balance”, but not “entity.listOfSubEntities[]”, since the download size increases quite quickly then. Is there a way to fetch “entity.listOfSubEntities[]” for the server to calculate, but then not pass it along to the REST response?

On your second question, @MetaProperty(related = “…”) is not working for this case. I think it might be because I’m trying to access properties of a sub entity, but I’m not sure.

I am using Platform 6.6.4.

A simplified version of my entities:


@BlahAnnotationsBlah
public class Policyholders extends BaseIdentityIdEntity {

    @Composition
    @OneToMany(mappedBy = "policyholders")
    protected List<Pdetail> lineItems;

    @Transient
    @MetaProperty(related = "lineItems")
    protected BigDecimal balance;

    public BigDecimal getBalance() {
        return lineItems
                .stream()
                .filter(Objects::nonNull)
                .map(Pdetail::getBalance)
                .reduce(BigDecimal.ZERO, BigDecimal::add);
    }
}

@BlahAnnotationsBlah
public class Pdetail extends BaseUuidEntity {

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "POLICYHOLDERS_ID")
    protected Policyholders policyholders;

    @Composition
    @OneToMany(mappedBy = "pdetail")
    protected List<Aroinv> invoiceItems;

    @Transient
    @MetaProperty(related = "invoiceItems")
    protected BigDecimal balance;

    public BigDecimal getBalance() {
        return invoiceItems
                .stream()
                .filter(Objects::nonNull)
                .map(Aroinv::getIbal)
                .reduce(BigDecimal.ZERO, BigDecimal::add);
    }
}

@BlahAnnotationsBlah
public class Aroinv extends BaseIdentityIdEntity {

    @Column(name = "ibal", nullable = false, precision = 10, scale = 2)
    protected BigDecimal ibal;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "PDETAIL_ID")
    protected Pdetail pdetail;

    public BigDecimal getIbal() {
        return ibal;
    }
}

I have attached two versions of my views - one that works (views.xml), and one that doesn’t (views-notWorking.xml).
Both have “balance” specified in the root, but the one that doesn’t work doesn’t specify to include “invoiceItems” and “invoiceItems.ibal”, which is the field used to determine “balance”.

The error I get is:

Caused by: java.lang.IllegalStateException: Cannot get unfetched attribute &#91;ibal&#93; from detached object ca.theaidplans.muaipm.entity.Aroinv-397395 &#91;detached&#93;.

views.xml (2.8K)

views-notWorking.xml (2.7K)

For example, in some view I might like to show that the balance for a policy holder is $XX.XX, but I don’t need to show the individual line item balances. Because I don’t need to show them, I would like to avoid downloading them to the client’s/user’s browser, since that takes time (and costs more data usage for the user). Of course, they still need to be downloaded to the server so that the balance can be calculated, but this is fine - so long as it doesn’t also get downloaded to the browser.

Now I understand your intention. Unfortunately, there is no easy way to strip loaded properties before sending them to clients.

I would strongly recommend making your balance attribute persistent, i.e. store it in the database and maintain its value using entity listeners. It looks like an overhead, but in fact it provides much better performance because reads usually prevail over modifications. And you solve your current problem without any effort.

That is unfortunate. But I will look into your recommendation - that will probably work just fine for me. Thanks much!