JPAContainer sorting

Maybe this is a silly question but I’m wondering how to set up
default
sorting on a JPAContainer.

In Vaadin tutorials you’ll often see something like this:

// Set up sorting if the natural order is not appropriate persons.sort(new String{"age", "name"}, (where persons is a
JPAContainer
)

As far as I can tell the comment on the code is misleading. The
sort()
method doesn’t “set up” anything; it
performs
a sort. Correct?

Basically I’m looking for something that will be part of my call to
refresh()
so that everytime I call
refresh()
the container knows how I want the rows sorted.

Right now I’m doing something like this:

void refreshPersonsData() {
    persons.refresh();
    persons.sort(new String{"age", "name"},
}

which works but I don’t like it because I’m assuming the DB is actually hit twice. … or how to understand this?

My example is JPAContainer on top of a RDBMS, in this case MySQL.

Hi Peter,

Maybe someone else can give you a better solution than this but in the meantime; I think what you’re looking for is to create a custom QueryModifierDelegate (by extending the DefaultQueryModifierDelegate) for the JPA container and override the orderByWillBeAdded or orderByWasAdded methods. Then you can add your own ordering before the query is sent to the DB.

However, this might throw the UI visually out of sync, as an orderBy is applied to the query even though the UI doesn’t show any ordering. So depening on your needs this may or may not be a problem.

If you need to track the ordering in the UI as well, then you’ll probably have to override the table sort() and implement your own thing to track the sorting state for the custom delegate and so that the sort commands from the table doesn’t hit the DB twice.

Hope this helps,

Best,

Thanks Petri. Appreciated.

As I read your reply you basically confirm my understanding. I’m a bit perplexed why this isn’t handled more elegantly in JPAContainer since default sorting would surely be a common requirment for many. My experience with Vaadin and JPAContainer so far has only been positive which is why my first thought is : “I must have misunderstood this”. :slight_smile: