JPQL ManyToMany查询问题

时间:2011-04-05 10:10:22

标签: hibernate orm jpa jpql hibernate-mapping

我的实体映射如下:

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

2 个答案:

答案 0 :(得分:0)

我不确定这个查询到底出了什么问题,但我建议你以简单的形式重写它,也许会有所帮助:

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)

答案 1 :(得分:0)

我最终使用子查询来获得所需的结果。