Last active
February 25, 2024 03:26
-
-
Save clxy/5e51e9eb000ca07b7a7adb6bde767ff0 to your computer and use it in GitHub Desktop.
Android Room Generic Dao
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.arch.persistence.db.SimpleSQLiteQuery; | |
import android.arch.persistence.db.SupportSQLiteQuery; | |
import android.arch.persistence.room.Dao; | |
import android.arch.persistence.room.Delete; | |
import android.arch.persistence.room.Insert; | |
import android.arch.persistence.room.OnConflictStrategy; | |
import android.arch.persistence.room.RawQuery; | |
import java.lang.reflect.ParameterizedType; | |
import java.util.List; | |
@Dao | |
public abstract class AppDao<T> { | |
@Insert(onConflict = OnConflictStrategy.REPLACE) | |
public abstract long save(T obj); | |
@Insert(onConflict = OnConflictStrategy.REPLACE) | |
public abstract long[] save(T... objs); | |
@Insert(onConflict = OnConflictStrategy.FAIL) | |
public abstract long insert(T obj); | |
@Insert(onConflict = OnConflictStrategy.FAIL) | |
public abstract long[] insert(T... objs); | |
@Delete | |
public abstract void delete(T obj); | |
public int deleteAll() { | |
SimpleSQLiteQuery query = new SimpleSQLiteQuery( | |
"delete from " + getTableName() | |
); | |
return doDeleteAll(query); | |
} | |
public List<T> findAllValid() { | |
SimpleSQLiteQuery query = new SimpleSQLiteQuery( | |
"select * from " + getTableName() + " where deleteFlag = 0 order by sortKey" | |
); | |
return doFindAllValid(query); | |
} | |
public T find(long id) { | |
SimpleSQLiteQuery query = new SimpleSQLiteQuery( | |
"select * from " + getTableName() + " where deleteFlag = 0 and id = ?", | |
new Object[]{id} | |
); | |
return doFind(query); | |
} | |
public String getTableName() { | |
Class clazz = (Class) | |
((ParameterizedType) getClass().getSuperclass().getGenericSuperclass()) | |
.getActualTypeArguments()[0]; | |
// tableName = StringUtil.toSnakeCase(clazz.getSimpleName()); | |
String tableName = clazz.getSimpleName(); | |
return tableName; | |
} | |
@RawQuery | |
protected abstract List<T> doFindAllValid(SupportSQLiteQuery query); | |
@RawQuery | |
protected abstract T doFind(SupportSQLiteQuery query); | |
@RawQuery | |
protected abstract int doDeleteAll(SupportSQLiteQuery query); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.swalloworks.rakurakukakeibo.system; | |
import android.arch.persistence.room.Dao; | |
import com.swalloworks.rakurakukakeibo.base.AppDao; | |
/** | |
* Remember to keep the generic/class information in your proguard-rules.pro | |
*/ | |
@Dao | |
public abstract class UserDao extends AppDao<User> { | |
} |
Cannot use unbound generics in Dao classes. If you are trying to create a base DAO, create a normal class, extend it with type params then mark the subclass with @dao.
My guess is that you are using a Dao class without generic/class information.
The AppDao above can NOT be used directly, just the UserDao can.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
hi, i'm using this method but i faced with this error:
Cannot use unbound generics in Dao classes. If you are trying to create a base DAO, create a normal class, extend it with type params then mark the subclass with @dao.
any suggestion why?