dinyar
dinyar

Reputation: 193

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.

Upvotes: 0

Views: 425

Answers (2)

dinyar
dinyar

Reputation: 193

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

Upvotes: 0

axtavt
axtavt

Reputation: 242686

There is a VALUE keyword that should allow you to something like this:

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

Upvotes: 1

Related Questions