MapJoin的where子句中的条件api集谓词 [英] criteria api set predicate in where clause for MapJoin

查看:94
本文介绍了MapJoin的where子句中的条件api集谓词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在设置以下查询的 where 子句时遇到了麻烦:

I'm having some trouble with setting the where clause of the following query:

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

我基本上是在尝试获取所有在component-Map中包含条目的配置,其键包含 abc,其值包含 def。
基于 http://blogs.oracle的示例代码,我希望这可以工作.com / ldemichiel / entry / java_persistence_2_0_proposed ,第 Maps 部分,但显然我缺少了一些内容。

I am basically trying to get all Configurations that contain an entry in the components-Map whose key contains "abc" and whose value contains "def". I expected this to work, based on the sample code from http://blogs.oracle.com/ldemichiel/entry/java_persistence_2_0_proposed, section Maps, but apparently I'm missing something.

实体具有以下结构:

@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中,假定Map密钥来自目标对象,如果您未使用@MapKey将目标字段设置为用于密钥的对象,则默认情况下,它将假定为对象的ID。在这种情况下,您的键是一个字符串,而Id是一个Long,所以我根本看不到它起作用吗?

Your code does not make sense. By default in JPA the Map key is assumed to come from the target object, and if you don't set the target field to use for the key using @MapKey then by default it is assumed to be the object's Id. In this case your key is a String and Id is a Long, so I can't see this working at all?

您需要提供一个@MayKey或一个@MapKeyColumn将键独立存储在联接表中。

You need to give a @MayKey, or a @MapKeyColumn to store the key independently in the join table.

这篇关于MapJoin的where子句中的条件api集谓词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆