hibernateinheritancejpajpqlejbql

JPQL: determine sub class type from super class join?


My question is very similar to this one:

How can I write a Hibernate Criteria query, for a super-class, and check for a certain sub-class?

..., except for one thing:

I'm referencing a super table/entity class (Round) which has two sub tables/entity classes (RankingRound and EliminationRound). I then create a JOIN:

SELECT
  ...
  ??? AS is_ranking_round
  ...
FROM Group gr
  JOIN gr.round rd
  ...
WHERE

Is there a way to find out the round type of the rd instance like the above in JPQL? (I don't seem to be able to translate the criterion to anything that works in JPQL.)


Solution

  • This works only with JPA 2.0 on. JPA 1 does not have TYPE.

    To get type as java.lang.Class:

    Select TYPE(rd)  FROM Group gr JOIN gr.round rd 
    

    Mapping type of the class to string:

    SELECT
        CASE TYPE(rd)
            WHEN RankingRound THEN 'RankingRound'
            WHEN EliminationRound THEN 'EliminationRound'
           ELSE 'Not mapped'
        END
    FROM Group gr JOIN gr.round rd