Requesting and Paging Through Multiple Unrelated Entity Types with Hibernate
Asked Answered
D

1

6

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?

Depreciation answered 23/1, 2012 at 14:44 Comment(0)
R
4

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.

Reese answered 23/1, 2012 at 14:52 Comment(1)
Correct - there will be no common base class. Your first suggestion was the only Hibernate-way I've thought of so far, and it doesn't look pretty. My only indecisiveness in between choosing one of those two methods is maintainability in adding more entities into the mix at a later time.Depreciation

© 2022 - 2024 — McMap. All rights reserved.