Hazelcast查询可以使用对象方法吗?

时间:2012-06-14 13:32:06

标签: java spring hazelcast spring-el

我在文档中找不到这个。

如果我在hazelcast缓存中有一个复杂的对象:

public class MyObj {
    private int something;
    private AnotherObject complexMember;
}

我可以这样做:

map.values(new SqlPredicate("something > 3 AND complexMember.someProperty > 3"));

我知道@Cachable注释使用SpEL这是可能的。有可能以某种方式以编程方式进行吗?

谢谢,

1 个答案:

答案 0 :(得分:5)

是的可能。请尝试以下内容;

public static void main(String[] args) throws Exception {
    final HazelcastInstance hz = Hazelcast.newHazelcastInstance(null);
    final IMap map = hz.getMap("test");
    map.put(1, new MyObj(1, new AnotherObject("value")));

    System.out.println(map.values(new SqlPredicate("something > 0 and complexMember.someString like 'val%'")));
}

static class MyObj implements Serializable {
    private int something;
    private AnotherObject complexMember;

    MyObj(final int something, final AnotherObject complexMember) {
        this.something = something;
        this.complexMember = complexMember;
    }
}

static class AnotherObject implements Serializable  {
    private String someString;

    AnotherObject(final String someString) {
        this.someString = someString;
    }
}