Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend Panache sort specification to explicitly support sorting by the size of an association of a JPA entity #31817

Open
mzuber opened this issue Mar 13, 2023 · 2 comments
Labels
area/panache kind/enhancement New feature or request

Comments

@mzuber
Copy link
Contributor

mzuber commented Mar 13, 2023

Description

The Panache library provides a utility class Sort to build and represent SQL sorting specifications. A Sort instance represents a list of columns to sort on, each with a direction to use for sorting, e.g.

Sort sort = Sort.by("name").and("age", Direction.Descending);
Sort sort2 = Sort.ascending("name", "age");
Sort sort3 = Sort.descending("name", "age");

In its current implementation, it is possible use the Sort class to not only sort by properties of a JPA entity (columns), but also by size of associations of the entity.

Given for example the JPA entities

@Entity
public class Person extends PanacheEntity {
    public String name;
    public String homeTown;
    @OneToMany
    public List<Dog> pets;
}

@Entity
public class Dog extends PanacheEntity {
    public String name;
    public String race;
    @ManyToOne
    public Person owner;
}

it is possible to "misuse" the interface of the Sort class to write a query like

Person.find("FROM Person p WHERE p.homeTown = :homeTown",
    Sort.by("SIZE(p.pets)", Sort.Direction.Ascending),
    Parameters.with("homeTown", "Berlin"));

It is great that this works, as this comes in especially handy when programmatically constructing the "sort property" for the query, but it doesn't feel like we are using the interface in the intended way here.

Therefore it would be great if the Sort class would provide an interface which more explicitly models this use case. For example via a method Sort.bySize(String association, Sort.Direction direction). In this case, the example above could be written like

Person.find("FROM Person p WHERE p.homeTown = :homeTown",
    Sort.bySize("p.pets", Sort.Direction.Ascending),
    Parameters.with("homeTown", "Berlin"));

Implementation ideas

Extend the Sort class with the methods

  • Sort.bySize(String association)
  • Sort.bySize(String... associations)
  • Sort.bySize(String association, Sort.Direction direction)
  • Sort.bySize(String association, Sort.NullPrecedence nullPrecedence)
  • Sort.bySize(String association, Sort.Direction direction, Sort.NullPrecedence nullPrecedence)

Extend the PanacheJpaUtil.toOrderBy() method (source) to construct an ORDER BY expression for the size of an association.

@mzuber mzuber added the kind/enhancement New feature or request label Mar 13, 2023
@quarkus-bot
Copy link

quarkus-bot bot commented Mar 13, 2023

/cc @FroMage (panache), @loicmathieu (panache)

@mzuber
Copy link
Contributor Author

mzuber commented Mar 13, 2023

I'm happy to provide a PR if you can imagine adding this functionality.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/panache kind/enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant