2011-04-05 71 views
0

我的实体映射如下:JPQL多对多查询问题

public class EntertainmentContentBean implements Serializable, Cloneable { 
. 
. 
. 
@ManyToMany 
    @Cache(usage = CacheConcurrencyStrategy.TRANSACTIONAL) 
    @JoinTable(schema = "etmt", name = "content_operator", joinColumns = @JoinColumn(name = "content_id"), inverseJoinColumns = @JoinColumn(name = "operator_id")) 
    public Set<Operator> getOperators() { 
     return operators; 
    } 
. 
. 
. 

} 

我已经在这里我想要检索基于以下标准EntertainmentContentBean记录的具体要求:

  • 如果没有运营商记录目前
  • 如果操作员记录存在,则操作员列表应包含操作员对象通过参数

我尝试下面的查询,但没有返回预期记录:

SELECT NEW MAP (content AS content, id AS record_id) 
     FROM EntertainmentContentBean contentEntry 
     WHERE (contentEntry.operators is empty or (select op from Operator op where lower(op.key) = lower(:operator)) MEMBER OF contentEntry.operators)) 
+0

什么是非常错误的?哪些记录被返回? – axtavt 2011-04-05 10:34:32

+0

记录是一个笛卡尔加入contentEntry.operators表 – 2011-04-05 11:07:50

回答

0

我结束了使用子查询来获得所需的结果。

0

我不知道究竟是什么不对的查询,但我建议你重写它的simplier形式,也许它将帮助:

SELECT DISTINCT NEW MAP (ce.content AS content, ce.id AS record_id) 
FROM EntertainmentContentBean ce LEFT JOIN ce.operators op 
WHERE op IS NULL OR lower(op.key) = lower(:operator) 
+0

嗨..它肯定简化了查询,但结果仍然是... :( – 2011-04-06 06:09:53