开发者

JPQL: determine sub class type from super class join?

开发者 https://www.devze.com 2023-03-17 11:43 出处:网络
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?

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 using a JPQL query instead of the Hibernate Criteria API (still Hibernate as a JPA provider though)

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.)


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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号