Sanjay
Sanjay

Reputation: 71

Ignite SqlQuery for complex java objects

In my cache I have a complex java object as below -

class Person{
  private Department d;
  ...
}

class Department {
  private Department code;
  ...
}

I am using below SQLQuery to read it:

SqlQuery<Short, BinaryObject> query = new SqlQuery<>(Person.class, "d.code = ?");
String args="101"; // department code
QueryCursor<Cache.Entry<Short, BinaryObject>> resultSet = personCache.query(query.setArgs(args))

I am getting below error:

Caused by: class org.apache.ignite.internal.processors.query.IgniteSQLException: Failed to parse query: SELECT "PERSON_CACHE"."PERSONENTITY"._KEY, "TPERSON_CACHE"."PERSONENTITY"._VAL FROM "PERSON_CACHE"."PERSONENTITY" WHERE id.code = ?

Am I doing anything wrong here ?

Upvotes: 0

Views: 1000

Answers (3)

Vladimir Ozerov
Vladimir Ozerov

Reputation: 46

You can access nested fields, but only if they were configured with QuerySqlField annotation in advance:

class Person{
    private Department d;
    ...
}

class Department {
    @QuerySqlField
    private Department code;
    ....
}

SqlQuery<Short, BinaryObject> query = new SqlQuery<>(Person.class, "code = ?");

Upvotes: 1

Sanjay
Sanjay

Reputation: 71

I resolved it by using predicate.

IgniteBiPredicate<Long, BinaryObject> predicate = new IgniteBiPredicate<Long, BinaryObject>() {
        @Override
        public boolean apply(Long e1, BinaryObject e2) {
          Person p= e2.deserialize();
          short s = (short) args[0];
          return p.getId().getCode == s;
        }
      };

Upvotes: 0

alamar
alamar

Reputation: 19313

Destructuring is not supported by Ignite SQL and there are no solid plans to implement it.

This means you can't peek into fields that are rich objects, maps, lists, etc. You should introduce a departmentId numeric field here.

Theoretically you could also try putting @QuerySqlField annotation on Department's field code, and then access it as CODE = ?. Your mileage may vary. I for one would like to hear about the result of such experiment.

Upvotes: 0

Related Questions