【问题标题】:criteria api set predicate in where clause for MapJoin条件 api 在 MapJoin 的 where 子句中设置谓词
【发布时间】:2012-02-05 19:24:49
【问题描述】:

我在设置以下查询的where 子句时遇到了一些问题:

CriteriaBuilder cb = em.getCriteriaBuilder();
  CriteriaQuery<Configuration> cq = cb.createQuery(Configuration.class);
  Root<Configuration> conf = cq.from(Configuration.class);
  MapJoin<Configuration, String, Component> mapJoin = conf.join(Configuration_.components, JoinType.LEFT); 
  cq.where(cb.and(cb.like(mapJoin.key(), "%abc%"), 
             cb.like(mapJoin.value().get(Component_.displayName), "%def%")));
  cq.select(pc);

我基本上是在尝试获取所有包含组件映射中的条目的配置,其键包含“abc”并且其值包含“def”。 根据http://blogs.oracle.com/ldemichiel/entry/java_persistence_2_0_proposedMaps 部分的示例代码,我希望这可以工作,但显然我遗漏了一些东西。

实体具有以下结构:

@Entity
public class Configuration{
  @Id
  protected Long id;       
  @OneToMany
   protected Map<String, Component> components;
}

@Entity
public class Component{
    @Id
    protected Long id;    
    protected String displayName;
}

提前感谢您,感谢您的帮助。

【问题讨论】:

    标签: jpa jpa-2.0 criteria criteria-api


    【解决方案1】:

    你得到什么错误?

    您的代码没有意义。默认情况下,在 JPA 中,假定 Map 键来自目标对象,如果您没有使用 @MapKey 将目标字段设置为用于键,则默认情况下假定它是对象的 Id。在这种情况下,您的密钥是 String 而 Id 是 Long,所以我根本看不到这个工作?

    你需要给一个@MayKey,或者一个@MapKeyColumn来将key独立存储在join table中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-06
      相关资源
      最近更新 更多