It's a few months that I started developing android application. I have worked for about two years with Spring framework and Hibernate. So, searching for an ORM tool for android I found the ormlite project. I found it very interesting and I decided to used it in my application.
All seems to work fine but I would ask to the experts some tips on developing android apps using ormlite in efficient and (if possible) elegant manner!
Lets start from domain classes.
All domain classes implements a fake DomainObject interface:
@DatabaseTable(tableName = "job")
public final class Job implements DomainObject{
@DatabaseField(generatedId = true, columnName="_id")
public Integer id;
@DatabaseField(index=true)
public Integer remoteDbid;
@DatabaseField
public Date downloadDate;
@DatabaseField
public Date assignedDate;
.....
}
Then I have a generic Dao Interface
public interface GenericDao <T extends DomainObject> {
T queryForId(Integer id);
List<T> queryForAll();
void save(T object);
void merge(T object);
void delete(T object);
}
implemented by:
public class GenericDaoORM <T extends DomainObject> extends OrmLiteSqliteOpenHelper implements GenericDao<T>{
private static final String LOG_TAG = GenericDaoORM.class.getSimpleName();
private final static int DATABASE_VERSION = 7;
private final Class<T> type;
protected Dao<T, Integer> dao = null;
protected GenericDaoORM(final Context context, final Class<T> type) {
super(context, FileConfig.DB_PATH, null, DATABASE_VERSION);
this.type = type;
if (dao == null) {
try {
dao = getDao(type);
} catch (SQLException e) {
if (LogConfig.ENABLE_ACRA){
ErrorReporter.getInstance().handleException(e);
}
if (LogConfig.ERROR_LOGS_ENABLED){
Log.e(LOG_TAG, "Can't get DAO for "+type.getName(), e);
}
}
}
}
@Override
public T queryForId(final Integer id) {
try {
return dao.queryForId(id);
} catch (SQLException e) {
if (LogConfig.ENABLE_ACRA){
ErrorReporter.getInstance().handleException(e);
}
if (LogConfig.ERROR_LOGS_ENABLED){
Log.e(LOG_TAG, "Can't get element with id "+id, e);
}
return null;
}
}
@Override
public List<T> queryForAll() {
try {
return dao.queryForAll();
} catch (SQLException e) {
if (LogConfig.ENABLE_ACRA){
ErrorReporter.getInstance().handleException(e);
}
if (LogConfig.ERROR_LOGS_ENABLED){
Log.e(LOG_TAG, "Can't get "+ type.getName() +" list", e);
}
return null;
}
}
@Override
public void save(final T object) {
try {
dao.create(object);
} catch (SQLException e) {
if (LogConfig.ENABLE_ACRA){
ErrorReporter.getInstance().handleException(e);
}
if (LogConfig.ERROR_LOGS_ENABLED){
Log.e(LOG_TAG, "Can't save "+ object.getClass().getName(), e);
}
}
}
@Override
public void merge(final T object) {
try {
dao.update(object);
} catch (SQLException e) {
if (LogConfig.ENABLE_ACRA){
ErrorReporter.getInstance().handleException(e);
}
if (LogConfig.ERROR_LOGS_ENABLED){
Log.e(LOG_TAG, "Can't update "+ object.getClass().getName(), e);
}
}
}
@Override
public void delete(final T object) {
try {
dao.delete(object);
} catch (SQLException e) {
if (LogConfig.ENABLE_ACRA){
ErrorReporter.getInstance().handleException(e);
}
if (LogConfig.ERROR_LOGS_ENABLED){
Log.e(LOG_TAG, "Can't delete "+ object.getClass().getName(), e);
}
}
}
@Override
public void onCreate(final SQLiteDatabase database, final ConnectionSource connectionSource) {
try {
TableUtils.createTableIfNotExists(connectionSource, Job.class);
} catch (SQLException e) {
if (LogConfig.ENABLE_ACRA){
ErrorReporter.getInstance().handleException(e);
}
if (LogConfig.ERROR_LOGS_ENABLED){
Log.e(LOG_TAG, "Cannot create Tables", e);
}
}
}
@Override
public void onUpgrade(final SQLiteDatabase database, final ConnectionSource connectionSource, final int oldVersion, final int newVersion) {
onCreate(database, connectionSource);
}
}
Each domain object has it's own Dao Interface defining extra methods (just in case other methods are needed than those defined in GenericDao):
public interface JobDao extends GenericDao<Job>{
Cursor getReceivedJobs();
...
}
and the relative implementation:
public final class JobDaoORM extends GenericDaoORM<Job> implements JobDao {
private static final String LOG_TAG = JobDaoORM.class.getSimpleName();
public JobDaoORM(final Context context) {
super(context, Job.class);
}
public Cursor getReceivedJobs() {
try{
final QueryBuilder<Job, Integer> queryBuilder = dao.queryBuilder();
queryBuilder.orderBy("reportDate", false);
queryBuilder.selectColumns(new String[]{"...", "...", ...});
final Where<Job, Integer> where = queryBuilder.where();
where.eq("executed", true);
final PreparedQuery<Job> preparedQuery = queryBuilder.prepare();
final AndroidCompiledStatement compiledStatement =
(AndroidCompiledStatement)preparedQuery.compile(connectionSource.getReadOnlyConnection(),StatementType.SELECT);
return compiledStatement.getCursor();
} catch (SQLException e) {
if (LogConfig.ENABLE_ACRA){
ErrorReporter.getInstance().handleException(e);
}
if (LogConfig.ERROR_LOGS_ENABLED){
Log.e(LOG_TAG, "getReceivedJobs()", e);
}
}
return null;
}
}
So my questions are...
1)Are this kind of implementation of domain/dao performant or have I introduced redundant and not necessary code?
2)Is this a good way to work with ormlite?
3)It a good practice to keep instance of each dao in a class that extends Application, or is better to keep an an instance of the dao on each activity or keep elsewhere?
I have also tryed to handle transaction but with no success.. I got an exception says that the table is locked.. I've create a method inside GenericDao returning a new instance of TransactionManager:
public interface GenericDao <T extends DomainObject> {
....
TransactionManager getTransactionManager();
}
public class GenericDaoORM <T extends DomainObject> extends OrmLiteSqliteOpenHelper implements GenericDao<T>{
...
@Override
public TransactionManager getTransactionManager(){
return new TransactionManager(getConnectionSource());
}
}
But when I execute the code in transaction updating DB i got an exception...
Thank you for your response!
Marco