User Defined Classes

Since matching and comparing expressions is executed via java's built-in equals and Comparable mechanisms, a user defined class can be matched and compared as well.

For example, suppose a user defined a class called Version, which encapsulates a major version and a minor version integers, and then implements a space class Document with a version property. The following example queries for a Document with a version greater than 2.5:

SQLQuery<Document> query = new SQLQuery<Document>(Document.class, "version > ?", new Version(2, 5));

In order for that query to execute correctly, the user-defined class should implement the following:

  • For equality (equals/not equals): the equals() and hashCode() methods.
  • For comparison (greater/less than): the java.lang.Comparable interface.

Here's an example of Version implementing both equality and comparison requirements:

public class Version implements Serializable, Comparable<Version> {
    private int major;
    private int minor;
    // Getters and setters are omitted for brevity.

    public boolean equals(Object obj) {
        if (obj == this)
            return true;
        if (!(obj instanceof Version))
            return false;
        Version other = (Version)obj;
        return this.major == other.major && this.minor == other.minor;

    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + major;
        result = prime * result + minor;
        return result;

    public int compareTo(Version other) {
        int result =, other.major);
        if (result == 0)
            result =, other.minor);
        return result;

In your Comparable class, make sure that the compareTo() and equals() methods always yield the same result, because the GigaSpaces query optimization depends on that equivalence. This is not the case, for example, where the BigDecimal.equals() method takes into account the number of trailing zeros, whereas the BigDecimal.compareTo() method ignores the trailing zeros.