JPA Criteria Lazy Container - Problem with Nested Properties

Hi Jean!

Im in troubles with Nested Properties.

If a nested property is null, it’s causing a null pointer exception, that I belive might be avoided by container. For example:

Person → Login Account → username.

Not every person needs to have a login account. It permits null on relationship.

On the Table, I wanna show the username, in the case that Person haves a Username, otherwise, shows the default value of property.

In this case, the, using addContainerProperty(loginAccount.username) causes a null pointer exception on the container.

And another little thing: Will be possible to order the table based on nested properties? For now, I can only order by directly properties.

Im using the 0.50 version, and CriteriaContainer class.

Nested properties are managed by Jakarta PropertyUtils; I will see what I can do to catch the NPE.

Concerning sorting, it has to be handled by the database – the very purpose of the lazy container is to allow large datasets.
So your nested property would have to be within a JPA Embeddable object, otherwise you won’t be able to sort on it as far as I know.
The sort would be a standard JPA Criteria sort.

Hi Jean. First of all, many thanks by atention. As always, we can count with your help :bashful:

I have a doubt with this:

I have this scenario:


public class TaskExecutor {

    private String name;

}

public class Task {
    private TaskExecutor executor;
}

I wanna sort by executor.name, but executor is not a @Embeddable class. In this case, I cant sort?

Sorry by this noob question!

Att,

I suggest you split the problem in two.

Imagine there is no Vaadin around. My reasoning is as follows: if the information is in the database, I can write a query to fetch and sort the information in the right order. As far as I know, pretty much any query can be expressed as a Criteria query. This query will involve a sort condition on your nested object.
You will be writing something along the lines of SELECT t.* FROM Task t, TaskExecutor e WHERE t.executor = e.id ORDER BY e.name

Once you have the query, you have two choices.

a) you use a BeanTupleContainer and display the properties directly (because BeanTupleItem allows you to reference all the fields of t and e directly)
b) you use a CriteriaContainer based on t, and rely on the nested property access to get to e.name

In both cases, the query does the sorting, not the container.
In case b) you are implicitly relying on the fact that the JPA implementation fetched the executor object (eager fetching). If you do not have eager fetching, then there is the very real risk of causing one additional query per row and that can spell disaster, performance wise.

In general, a) is a simpler strategy and more performant, unless you actually need to create Java objects for each executor. In other words, unless you need to call methods on the Java object, do not create java objects.

Understood…

I will try with BeanTupeContainer.

I know that I can order by the nested property. The strange, is because the header of Table is not “clickable” to order dynamically, as the normal properties is.

I will verify if this behavior works with BeanTupleContainer!

Many thanks!

Att,