0
votes

I have a domain layer and data layer. Is it possible to do this?

Domain: User.java UserDao.java

Data: UserRoomEntity.java UserRoomDao.java

I want all my data sources to implement the UserDao. When i make UserRoomDao implements UserDao interface i get errors that i should annotate the UserDao.java function with @insert and the rest of Room annotations. Is it possible to do this with Room?

@Dao
public abstract class UserRoomDao implments UserDao{

    @Insert
    public abstract void insert(User...users);

    @Update
    public abstract void update(User...users);

    @Delete
    public abstract void delete(User...users);

public interface UserDao {


    public void insert(UserModel... userModels);

    public void delete(UserModel... userModels);

    public void update(UserModel... userModels);

}

i get this error in the UserDao.

error: An abstract DAO method must be annotated with one and only one of the following annotations: Insert, Delete, Query, Update, RawQuery

3
Could you please provide some code? - Arka Prava Basu
please give some code of what you want to achieve, even if it doesn't work, just show what you want to achieve - a_local_nobody
i have added the code. - Number 13

3 Answers

0
votes

You would have to make UserRoomDao class abstract and the mark the methods in UserDao abstract so Room will provide the implementations for them.

0
votes

I extend the UserDao with UserRoomDao interface and override the methods. It works for me.

 public interface UserDao {

    Flowable<User> getUser();

    Completable insertUser(User user);

    void deleteAllUsers(); 
}

@Dao
public interface UserRoomDao extends UserDao {

    @Query("SELECT * FROM Users LIMIT 1")
    Flowable<User> getUser();

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    Completable insertUser(User user);

    @Query("DELETE FROM Users")
    void deleteAllUsers();
}

Make sure to return UserRoomDao from UsersDatabase

@Database(entities = {User.class}, version = 1)
public abstract class UsersDatabase extends RoomDatabase {

    public abstract UserRoomDao userRoomDao();
}
0
votes

You are missing @Dao anotation in your UserDao class. Also you missed abstract keyword. so full example like :

@Dao
public abstract class UserRoomDao extend UserDao{

    @Insert
    public abstract void insert(User...users);

    @Update
    public abstract void update(User...users);

    @Delete
    public abstract void delete(User...users);
}

and change interface to class. (You can keep both UserDao and UserRoomDao as interface)

public abstract class UserDao {

   @Insert
    public void insert(UserModel... userModels);
    @Update
    public void delete(UserModel... userModels);
   @Delete
    public void update(UserModel... userModels);

}

Suggestion: Make generic dao class so other dao can use it. Like:

@Dao 
public abstract interface MyGenericDao<T>{
        @Insert(onConflict = OnConflictStrategy.REPLACE)
        public abstract long insertOrUpdate(T row);

        @Update
        public abstract int update(T... objects);

        @Delete
        public abstract int delete(T... objects);
    }

Now you can use this generic dao in your others dao so for inserting or updating (Generic work) will be covered by generic dao not need to implement any same method again.