Search code examples
javajpajpqlopenjpa

Querying Entities with maps in OpenJPA


I have the following Entities

@Entity
public class Conversation implements Serializable {

    @Id
    private int Id;

    @Column
    private Alias AliasA;

    // SNIP
}

and

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

and

@Entity
public class Recipient implements Serializable {

    @Id
    @GeneratedValue
    private long id;

    @Column
    private String address;

    @Column
    private RecipientStatus status;

    @ManyToOne
    private Alias alias;
}

And I would like to make something like the following JPQL query

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

Where :sender is in the key of my Map. The MEMBER OF keyword however only seems to work with Sets and not with Maps. I believe that JPA 2.0 should offer the KEY keyword, but this doesn't seem to be implemented in OpenJPA yet. Is there an alternative to this?

Update: Added information to clarify my question.


Solution

  • While axtavt's answer gave me the hint I needed, the answer was actually, that the error checking in IntelliJ 10.5.4 is not to be trusted in this case.

    The KEY keyword does indeed work and the correct query was

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