2012-02-05 60 views
0

我遇到一些麻烦设置以下查询where条款:标准的API集谓词的where子句MapJoin

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_proposed,部分Maps的示例代码,但显然我错过了一些东西。

实体具有以下结构:

@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; 
} 

预先感谢您,感谢任何帮助。

回答

1

你会得到什么错误?

您的代码没有意义。默认情况下,在JPA中,假设Map键来自目标对象,并且如果您没有使用@MapKey为该键设置目标字段,那么默认情况下它被假定为对象的Id。在这种情况下,你的密钥是一个字符串,身份证是一个长,所以我看不到这个工作呢?

您需要提供@MayKey或@MapKeyColumn以将关键字独立存储在连接表中。

+0

谢谢,这让我走向了正确的方向 – glasspill 2012-02-07 20:51:20