8

这工作正常:

 public Predicate toPredicate(Root<Campaign> root,
    CriteriaQuery<?> query, CriteriaBuilder cb) {
    return root.get("campState").get("statusId").in(campStatus);
}

但我改为:</p>

 return  root.get(Campaign_.campState).get(CampState_.campId).in(campStatus).

并抛出异常:</p>

编辑: org.hibernate.ejb.criteria.path.Abs​​tractPathImpl.get 的 NullPointerException

  @StaticMetamodel(Campaign.class)
  public class Campaign_ {
  public static volatile SingularAttribute<Campaign, CampState> campState;
  public static volatile SingularAttribute<Campaign, Long> campId;
  }

  @StaticMetamodel(CampState.class)
  public class CampState_ {
  public static volatile SingularAttribute<CampState, String> statusId;
  }

  Campaign.class
  @ManyToOne(cascade = {CascadeType.REFRESH, CascadeType.MERGE})
  @JoinColumn(name = "camp_status")
  public CampState getCampState() {
    return campState;
  }

  public void setCampState(CampState campState) {
    this.campState = campState;
  }
4

2 回答 2

2

检查这个答案

似乎元模型和类应该在同一个包中

于 2013-11-20T13:52:44.367 回答
0

给定两个查询对不同的属性进行操作。第二个查询无法编译,因为 CampState_ 没有属性 campId(Campaign_ 确实有这样的属性。

相当于第一个具有静态元模型的查询是:

root.get(Campaign_.campState).get(CampState_.statusId).in(campStatus).
于 2013-08-18T17:42:41.803 回答