Request and paging through several unrelated hibernate entity types

I have a strange business requirement.

We have several unrelated entity types that will need to be displayed in a unified list, with some basic information from the entity sorted by the only field on which they are all guaranteed, DATE. These objects may or may not even be in the same database. A result set should be available for pages.

Is there any possible way to achieve this through Criteria, HQL or some sane means?

+6
source share
1 answer

Typically, you allow all of these classes to distribute a common base class and use the Hibernate polymorphic query. From your description this is not possible.

Of course, if you want to go along the Hibernate path, you need to first get the size of each unrelated table, determine which table records the records on the requested page (or perhaps several), and manually extract the correct page. This is really cumbersome and definitely should be hidden under some deep DAO.

It seems that only a reasonable solution is a good old SQL with UNION and matching your own query with domain objects. Hibernate supports native queries.

+4
source

Source: https://habr.com/ru/post/906672/


All Articles