bvulaj
bvulaj

Reputation: 5123

Requesting and Paging Through Multiple Unrelated Entity Types with Hibernate

I have an odd business requirement.

We have multiple, 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 they are all guaranteed to have, DATE. These entities may or may not even be in the same database. The result set needs to be pageable.

Is there any feasible way of achieving this through Criteria, HQL or some sane means?

Upvotes: 6

Views: 917

Answers (1)

Tomasz Nurkiewicz
Tomasz Nurkiewicz

Reputation: 340723

Normally you would let all these classes extend common base class and use polymorphic Hibernate query. From your description this doesn't seem to be feasible.

Of course if you want to go the Hibernate way, you would have to first fetch the size of each unrelated table, determine in which table do the records in requested page lie (or maybe in several ones) and manually fetch proper page. This is really cumbersome and definitely should be hidden under some deep DAO.

Looks like do only sane solution is the good old SQL with UNION and mapping native query to your domain objects. Hibernate supports native queries quite well.

Upvotes: 4

Related Questions