I am using ORMLite
framework on an android project and I am stuck while using OpenHelperManager.getHelper()
on multiple repo
classes.
Here is a brief of my code structure :-
DatabaseHelper
class which is responsible for creating database
and tables
public class DatabaseHelper extends OrmLiteSqliteOpenHelper{
public static String DATABASE_NAME = "muffet.db";
public static int DATABASE_VERSION = 5;
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION, R.raw.ormlite_config);
}
@Override
public void onCreate(SQLiteDatabase database, ConnectionSource connectionSource) {
// Creating tables
try {
TableUtils.createTable(connectionSource, Category.class);
TableUtils.createTable(connectionSource, Transaction.class);
TableUtils.createTable(connectionSource, Budget.class);
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
public void onUpgrade(SQLiteDatabase database, ConnectionSource connectionSource, int oldVersion, int newVersion) {
try {
TableUtils.dropTable(connectionSource, Category.class, true);
TableUtils.dropTable(connectionSource, Transaction.class, true);
TableUtils.dropTable(connectionSource, Budget.class, true);
onCreate(database, connectionSource);
} catch (SQLException e) {
e.printStackTrace();
}
}
}
TransactionRepo
which is responsible for accessing database.
public class TransactionRepo extends DatabaseHelper {
// private Dao<Transaction, Integer> transactionDao = null;
private RuntimeExceptionDao<Transaction, Integer> transactionRuntimeDao = null;
public TransactionRepo(Context context) {
super(context);
}
public RuntimeExceptionDao<Transaction, Integer> getTransactionRuntimeDao(){
if(transactionRuntimeDao == null){
transactionRuntimeDao = getRuntimeExceptionDao(Transaction.class);
}
return transactionRuntimeDao;
}
/**
* Save Transaction to database
*/
public void save(Transaction transaction){
try {
transactionRuntimeDao = getTransactionRuntimeDao();
transactionRuntimeDao.createOrUpdate(transaction);
}catch(Exception e){
e.printStackTrace();
}
}
/**
* Get all list of data
*/
public List<Transaction> getAll(){
try{
transactionRuntimeDao = getTransactionRuntimeDao();
return transactionRuntimeDao.queryForAll();
}catch (Exception e){
e.printStackTrace();
return null;
}
}
}
TransactionServiceImpl
which is a service class for getting data from the TransactionRepo
public class TransactionServiceImpl implements TransactionService {
TransactionRepo transactionRepo;
@Override
public void save(Transaction transaction, Activity activity) {
transactionRepo = OpenHelperManager.getHelper(activity, TransactionRepo.class);
transactionRepo.save(transaction);
OpenHelperManager.releaseHelper();
}
@Override
public List<Transaction> getAll(Activity activity) {
transactionRepo = OpenHelperManager.getHelper(activity, TransactionRepo.class);
List<Transaction> transactionList = transactionRepo.getAll();
OpenHelperManager.releaseHelper();
return transactionList;
}
}
It works fine when I run it. But when I try to access some another repository
class which extends DatabaseHelper
, and use the OpenHelperManager.getHelper()
like for example :-
CategoryRepo categoryRepo = OpenHelperManager.getHelper(activity, CategoryRepo.class);
I get error as java.lang.IllegalStateException: Helper class was class com.muffet.persistence.repository.TransactionRepo but is trying to be reset to class com.muffet.persistence.repository.CategoryRepo
This clearly means that the OpenHelperManager
has saved instance of TransactionRepo
and when I try to get and set it as CategoryRepo
it is unable to cast or something like that.
How can I solve this issue?
Thank you
Aucun commentaire:
Enregistrer un commentaire