在OpenJPA中使用地图查询实体

时间:2012-04-18 11:25:43

标签: java jpa jpql openjpa

我有以下实体

@Entity
public class Conversation implements Serializable {

    @Id
    private int Id;

    @Column
    private Alias AliasA;

    // SNIP
}

@Entity
public class Alias implements Serializable {

    @Id
    private String alias;

    @Column
    private String personalName;

    @OneToMany(mappedBy = "alias", cascade = CascadeType.ALL)
    @MapKeyColumn(name="address")
    private Map<String, Recipient> recipients = new HashMap<String, Recipient>();
}

@Entity
public class Recipient implements Serializable {

    @Id
    @GeneratedValue
    private long id;

    @Column
    private String address;

    @Column
    private RecipientStatus status;

    @ManyToOne
    private Alias alias;
}

我想制作类似下面的JPQL查询

SELECT conversation FROM Conversation conversation WHERE :sender MEMBER OF conversation.aliasA.recipients AND conversation.adId=:adID

:sender位于我的地图键中。但是,MEMBER OF关键字似乎只适用于集合,而不适用于地图。我相信JPA 2.0应该提供KEY关键字,但这似乎还没有在OpenJPA中实现。有替代方案吗?

更新:添加信息以澄清我的问题。

2 个答案:

答案 0 :(得分:1)

有一个VALUE关键字可以允许您这样的内容:

SELECT c FROM Conversation c JOIN c.aliasA a JOIN a.recepients r 
WHERE VALUE(r) = :sender AND conversation.adId=:adID

答案 1 :(得分:0)

虽然axtavt的答案给了我所需的提示,但答案实际上是,在这种情况下,IntelliJ 10.5.4中的错误检查不值得信任。

KEY关键字确实有效,正确的查询是

SELECT conversation FROM Conversation conversation JOIN conversation.aliasA.recipients recipients WHERE KEY(recipients) = :senderAddress AND conversation.adId=:adID