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

帅比萌擦擦* 提交于 2019-11-30 07:37:11

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

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);
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!