How to refer to a subclass specific field in a CriteriaQuery where the super class is queried?

前端 未结 2 911
离开以前
离开以前 2020-12-30 08:43

I\'m trying to achieve something like the following, using the JPA Criteria API:

SELECT b FROM Box b JOIN SpecialItem s WHERE s.specialAttr = :specialAttr
         


        
2条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-30 09:39

    If using JPA 2.1 you might use

    "SELECT b FROM Box b WHERE TREAT(b.item as SpecialItem).specialAttr = :specialAttr"
    

    or

    CriteriaQuery q = cb.createQuery(Box.class);
    Root box= q.from(Box.class);
    Join order = box.join("item");
    q.where(cb.equal(cb.treat(order, SpecialItem.class).get("specialAttr"),
        qb.parameter(String.class, "specialAttr")));
    q.select(Box);
    

提交回复
热议问题