I have an Entity MyEntity
which has an ElementCollection
Map in it as shown below.
The Map<String, MyEmbeddableObject>
, values are an Embeddable object MyEmbeddableObject
and keys are 'String'.
The db tables and JPA mapping everything is working correctly in system currently. I just need a query to get the required info.
Assuming that MyEmbeddableObject
has some 'String' properties in it, is there a way to write a JPA NamedQuery so that I can filter the results based on comparison of properties from MyEmbeddableObject
?
Also the query should return (the select clause) MyEmbeddableObject
and not MyEntity
. findMyEmbeddableObjectByAttr1
is what I have tried and doesn't work. It complains the JPA Query Grammar is wrong.
I could get it done using Native query, but ideally I want to be able to do it with JPA NamedQuery for all the disadvantages associated with Native query.
@Entity
@NamedQueries({
@NamedQuery(name = "findMyEmbeddableObjectByAttr1",
query = "Select n from MyEntity v, in (v.myMap) n WHERE n.attr1 = :someUserPassedValue")
})
public class MyEntity {
@ElementCollection
@MapKeyColumn(name="someKey")
private Map<String, MyEmbeddableObject> myMap;
}
@Embeddable
public class MyEmbeddableObject {
private String attr1;
private String attr2;
}
All my SO searches and Google searches showed how one can write query to filter by Map's Keys or by comparing the entire Map value object. Comparison of Map value is an easy option if values are primitive type like String, int. But in my case its an Complex object.
These SO questions are the closest I could get to answers :
Execute "MEMBER OF" query against 'ElementCollection' Map fields in JP-QL (JPA 2.0)
LIKE query on values of Map ElementCollection in entity in HQL
Any help, greatly appreciated! Thanks.