Throw an exception when a mapped superclass is queried

Description

The attached test case works, giving the SQL:

select concrete2x0_.entityId as col_0_0_ from CON2 concrete2x0_ order by concrete2x0_.entityId ASC

select concrete1x0_.entityId as col_0_0_ from CON1 concrete1x0_ order by concrete1x0_.entityId ASC

and a warning: HHH000180: FirstResult/maxResults specified on polymorphic query; applying in memory!

Limits are not specified in the test case, regardless, this can be done in the database as:

(select entityId from CON2
union all
select entityId from CON1)
order by entityId ASC

And even with limits and sort conditions it can be done as:

select * from (
(select * from (select entityId from CON2 order by entityId ASC) where rownum <= 10)
UNION ALL
(select * from (select entityId from CON1 order by entityId ASC) where rownum <= 10)
) where rownum <= 10 order by entityId ASC

and also more generally with LIMIT and more sort columns, I believe.

Environment

Windows 7 Professional SP1, Oracle 12c, jdk-8u73-windows-x64

Activity

Show:
Gail Badner
March 21, 2016, 11:12 PM

Your test case does limit the number of results with: queri.setMaxResults(10). When that line is commented out, the warning is not logged.

More importantly, your named query is on a class that is a mapped superclass.

JPA 2.1, 2.11.2 Mapped Superclasses, says:

"A mapped superclass, unlike an entity, is not queryable and must not be passed as an argument to
EntityManager or Query operations."

Hibernate should throw an exception in this case.

Nikolas Andersen
March 22, 2016, 9:53 AM

Ah, OK. In that case, how about this as an enhancement, as it seems almost supported already?
The sorting/limiting can be done fully in the database and it fits the concept of mapping object hierarchies to relational tables.

Alternatively, how would I query a hierarchy of objects in Hibernate as, unless I am mistaken, I can't guarantee to query a single concrete type under a mapped superclass unless it has either a supertype or a subtype (HHH-10628)?

Basically I need to be able to either:

  • Return results from multiple tables with the combined result sorted by a common column (on the abstract supertype) and limited.

or

  • Return ordered/limited results for each concrete type in the hierarchy individually.

Thank you!

Assignee

Unassigned

Reporter

Nikolas Andersen

Fix versions

None

backPortable

None

Suitable for new contributors

None

Requires Release Note

None

Pull Request

None

backportDecision

None

Components

Affects versions

Priority

Minor
Configure