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

前端 未结 2 912
离开以前
离开以前 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:33

    I just want to extend the answer of Chris for the Criteria API with generated meta-model.

    CriteriaQuery<Box> q = cb.createQuery(Box.class);
    Root<Box> box= q.from(Box.class);
    Join<Box, Item> order = box.join(Box_.item);
    q.where(cb.equal(cb.treat(order, SpecialItem.class).get(SpecialItem_.specialAttr), "searchValue");
    q.select(Box);
    
    0 讨论(0)
  • 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<Box> q = cb.createQuery(Box.class);
    Root<Box> box= q.from(Box.class);
    Join<Box, Item > order = box.join("item");
    q.where(cb.equal(cb.treat(order, SpecialItem.class).get("specialAttr"),
        qb.parameter(String.class, "specialAttr")));
    q.select(Box);
    
    0 讨论(0)
提交回复
热议问题