Search code examples
cachinghazelcastpredicatehazelcast-imappredicatebuilder

How to fetch entities from Hazelcast map based on a field in its child entity


I'm using Hazelcast 3.12.6 version in one of my services to cache an entity (Parent) which has a child entity(Child) in it. Something like below:

public class Parent { 
    private String parentField;
    private Child child;
    //Getters and Setters
}

public class Child { 
   private String childField;
   //Getters and Setters
}

Now, I have a requirement to fetch all parent entries based on childField in child entity. So, can anyone please tell me how to achieve this using com.hazelcast.query.Predicate . I'm looking for something like below:

IMap<String, Parent> parentIMap = hazelcastInstance.getMap("parent");
Predicate predicate = Predicates.equal("child.childField", "somevalue");
parentIMap.values(pagingPredicate);

Any help is greatly appreciated. Thanks in advance.


Solution

  •     parentIMap.put("1", new Parent("Father",new Child("John")));
        parentIMap.put("2", new Parent("Father",new Child("Maria")));
        parentIMap.put("3", new Parent("Mother",new Child("John")));
        parentIMap.put("4", new Parent("Mother",new Child("Maria")));
    

    Given that set of data above, following queries will return parent entries for John

        Collection values = parentIMap.values(new SqlPredicate("child.childField =John"));
        Collection values = parentIMap.values(Predicates.equal("child.childField","John"));