ObjectDB ObjectDB

Query over the keySet of a map field with collection parameter

#1

It is possible to execute a query over a keySet of a map field with comparing to a collection parameter?

        Set<String> values = new HashSet<>();
        values.add("c2");

        String query = //
            "SELECT e " + "FROM " + EntityParent.class.getName() + " e "
                + "WHERE e.map member of ?1";

        TypedQuery<EntityParent> q = em.createQuery(query, EntityParent.class);
        q.setParameter(1, values);
        List<EntityParent> result = q.getResultList();


    @Entity
    public static class EntityParent {

        @Basic
        String value;

        public EntityParent(String name) {
            value = name;
        }

        @OneToMany (cascade = CascadeType.ALL)
        public Map<String, EntityChild> map = new HashMap<>();
    }


    @Entity
    public static class EntityChild {

        @Basic
        String value;

        public EntityChild(String name) {
            value = name;
        }

    }
edit
delete
#2

Using map values in queries is easier. Therefore, if the keys of the map field of EntityParent, i.e.

 public Map<String, EntityChild> map = new HashMap<>();

are also available in the map value, e.g. the field value of EntityChild, you can try:

SELECT e FROM EntityParent e JOIN e.map child WHERE child.value member of ?1
ObjectDB Support
edit
delete
#3

I know this solution and this would also be also my first solution.

But child.value and parent.map.keys have different contents. Then the EntityChild needs a new field with the content of map.keys and a db migration is necessary.

I need a query over the map.keys with a parameter which is a collection of strings.

SELECT e FROM EntityParent e JOIN e.map key WHERE key member of ?1
Set<String> params = new HashSet<>();
params.add("c2");
q.setParameter(1, params);

 

edit
delete
#4

To help you with this need, a new build, 2.8.7_07 implements a new query method: containsAnyKey.

Please check the following query with this new build:

SELECT e FROM EntityParent e WHERE e.map.containsAnyKey(?1)
ObjectDB Support
edit
delete
#5

works as expected, thank you very much

edit
delete

Reply

To post on this website please sign in.