【问题标题】:Hibernate Criteria Subquery RestrictionsHibernate Criteria 子查询限制
【发布时间】:2021-02-03 09:26:49
【问题描述】:

我有一个条件查询和一个子查询,我想将条件查询的限制应用于子查询。以下是我迄今为止尝试过的。

CriteriaQuery<Object> query = cb.createQuery(Object.class);
Root<Entity1> entityRoot = query.from(Entity1.class);
List<Predicate> conditions = new ArrayList<>();
conditions.add(cb.equal(entityRoot.get("relatedEntity").get("id"), 2));
conditions.add(cb.or(cb.isNull(entity1Entity2Join.get("columnName")), cb.equal(entity1Entity2Join.get("columnName"), true)));
query.where(conditions.toArray(new Predicate[]{})).distinct(true);

//Subquery
Subquery<Object> subQuery = cb.createQuery().subquery(Object.class);
subQuery.where(query.getRestriction()); // I want to apply the criteria query restrictions here but Hibernate is throwing exception of invalid path.

任何想法都将不胜感激。

【问题讨论】:

    标签: java hibernate jpa jpa-2.0 criteria-api


    【解决方案1】:

    你得到这个错误并且操作是正确的是正常的,最后你试图在子查询的where中建立一个你没有定义任何东西的新查询的一些条件,为了解决它尝试改变这一行

    Subquery <Object> subQuery = cb.createQuery().subquery(Object.class);
    

    为此

    Subquery <Object> subQuery = query.subquery(Object.class);
    

    从您已经创建的查询创建子查询,而不是从中创建新查询和子查询

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-03-04
      • 2012-02-15
      • 2016-09-08
      • 1970-01-01
      • 2019-04-01
      • 1970-01-01
      • 2012-01-29
      相关资源
      最近更新 更多