Using ORMLite 4.48 on Android, consider the following user table ...
@DatabaseTable(tableName = "users")
public class User {
@DatabaseField(columnName = Column.UUID)
protected String uuid;
@DatabaseField(columnName = Column.FIRST_NAME)
protected String firstName = null;
@DatabaseField(columnName = Column.LAST_NAME)
protected String lastName = null;
@DatabaseField(columnName = Column.ADDRESS, dataType = DataType.SERIALIZABLE)
protected Address address = null;
}
(The address class is a human readable address translated from lat / lon)
... with the following query ...
List<User> users = getDbHelper().getDao(User.class)
.queryBuilder()
.where()
.isNotNull(User.Column.ADDRESS)
.query();
... which throws this exception:
java.sql.SQLException: Field 'address' is of data type com.j256.ormlite.field.types.SerializableType@42ac7650 which can not be compared
at com.j256.ormlite.stmt.query.BaseComparison.<init>(BaseComparison.java:27)
at com.j256.ormlite.stmt.query.IsNotNull.<init>(IsNotNull.java:19)
at com.j256.ormlite.stmt.Where.isNotNull(Where.java:315)
...
Apparently, the sentence IS NOT NULLcannot be used in a field that is stored as DataType.SERIALIZABLE.
Digging through ORMLite code, the BaseComparison constructor checks to see if fieldType is comparable:
https://github.com/j256/ormlite-core/blob/master/src/main/java/com/j256/ormlite/stmt/query/BaseComparison.java
protected BaseComparison(String columnName, FieldType fieldType, Object value, boolean isComparison)
throws SQLException {
if (isComparison && fieldType != null && !fieldType.isComparable()) {
throw new SQLException("Field '" + columnName + "' is of data type " + fieldType.getDataPersister()
+ " which can not be compared");
}
...
}
Obviously SerializableType.isComparable()returns false:
https://github.com/j256/ormlite-core/blob/master/src/main/java/com/j256/ormlite/field/types/SerializableType.java
@Override
public boolean isComparable() {
return false;
}
, , . Where.isNull() Where.isNotNull()? SQLite ORMLite ?