John Ernest Guadalupe John Ernest Guadalupe - 6 months ago 14
Java Question

I can't retrieve data from sqlite db using Ormlite?

My model has a

java.sql.Date
variable and I am able to save just fine.

But whenever I try to retrieve a row it throws this error:

Caused by: java.lang.IllegalArgumentException: field model.date_time_created has type
java.sql.Date, got java.util.Date
at java.lang.reflect.Field.set(Native Method)
at java.lang.reflect.Field.set(Field.java:557)
at com.j256.ormlite.field.FieldType.assignField(FieldType.java:576)
       at com.j256.ormlite.stmt.mapped.BaseMappedQuery.mapRow(BaseMappedQuery.java:71)
        at com.j256.ormlite.stmt.SelectIterator.getCurrent(SelectIterator.java:270)
        at com.j256.ormlite.stmt.SelectIterator.nextThrow(SelectIterator.java:161)
        at com.j256.ormlite.stmt.StatementExecutor.query(StatementExecutor.java:202)
        at com.j256.ormlite.stmt.StatementExecutor.queryForAll(StatementExecutor.java:118)
        at com.j256.ormlite.dao.BaseDaoImpl.queryForAll(BaseDaoImpl.java:241)


My method to get a list :

public List<Model> getModelList() {
try {
return getHelper().getModelDao().queryForAll();
} catch (SQLException e) {
Timber.e(ErrorUtils.getExceptionString(e));
return null;
}
}


Model is a placeholder

Answer

IllegalArgumentException: field model.date_time_created has type java.sql.Date, got java.util.Date

The problem turned out to be a bug in ORMLite. Under Android/SQLite we have to convert all dates to be strings and the conversion from java.sql.Date was broken. Switching to java.util.Date will work.

This will be fixed in version 5.0. See: https://github.com/j256/ormlite-android/issues/51