Reputation: 4327
Im new of Room library and want to make simple project .
Here is entity class.
@Entity(tableName = "user")
public class User {
@PrimaryKey
private Integer id;
private String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Here is dao class.
@Dao
public interface UserDao extends Dao {
@Query("SELECT * FROM user")
List<User> getAll();
@Insert
void insert(User users);
}
Here is AppDatabase class.
@Database(entities = {User.class}, version = 1)
public abstract class AppDatabase extends RoomDatabase {
private static AppDatabase INSTANCE;
public abstract UserDao userDao();
public static AppDatabase getAppDatabase(Context context) {
if (INSTANCE == null) {
INSTANCE =
Room.databaseBuilder(context.getApplicationContext(), AppDatabase.class, "database-test")
.allowMainThreadQueries()
.build();
}
return INSTANCE;
}
public static void destroyInstance() {
INSTANCE = null;
}
}
And here is my mainactivity.
User user = new User();
user.setName("Test");
user.setId(2);
AppDatabase.getAppDatabase(this).userDao().insert(user);
AppDatabase.getAppDatabase(this).userDao().getAll();
But when I run this code above . I get error behind below .
Error:(14, 8) error: Abstract method in DAO must be annotated with interface android.arch.persistence.room.Query AND interface android.arch.persistence.room.Insert
I search it but cannot find anything . Thanks for any suggestion .
Upvotes: 3
Views: 4502
Reputation: 1415
For my need , i've used an abstract class like this :
public interface IMainDao {
String getId();
}
@Dao
public abstract class UserDao extends IMainDao {
@Query("SELECT * FROM user")
List<User> getAll();
//from IUserDao
@Override
public String getId() {
return "";
}
}
Upvotes: -2
Reputation: 3035
Your DAO interface shouldn't extend Dao interface.
@Dao
public interface UserDao {
@Query("SELECT * FROM user")
List<User> getAll();
@Insert
void insert(User users);
}
Upvotes: 4