Home > Mobile >  Room dao cannot resolve table name and method
Room dao cannot resolve table name and method

Time:08-27

I'm trying to implement a simple database using Room and Dao, this is what I did
My entity:

@Entity(tableName = "notes")
public class Note implements Serializable {

    @PrimaryKey(autoGenerate = true)
    private int id;

    @ColumnInfo(name = "title")
    private String title;

    @ColumnInfo(name = "date_time")
    private String datetime;

    @ColumnInfo(name = "subtitle")
    private String subtitle;

    @ColumnInfo(name = "note_text")
    private String noteText;

    @ColumnInfo(name = "image_path")
    private String imagePath;

    @ColumnInfo(name = "color")
    private String color;

    @ColumnInfo(name = "website")
    private String website;

    @NonNull
    @Override
    public String toString() {
        return title   " : "   datetime;
    }
}

I have also generated all getters and setters in the entity but I don't include here because it's very long.
My Dao interface:

@Dao
public interface NoteDAO {
    @Query("SELECT * FROM notes ORDER BY id DESC")
    List<Note> getAllNotes();

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    void insertNote(Note note);

    @Delete
    void deleteNote(Note note);
}

My database class:

@Database(entities = Note.class, version = 1, exportSchema = false)
public abstract class NoteDatabase extends RoomDatabase {

    private static NoteDatabase noteDatabase;

    public static synchronized NoteDatabase getDatabase(Context context){
        if (noteDatabase == null){
            noteDatabase = Room.databaseBuilder(
                    context,
                    NoteDatabase.class,
                    "note_db"
            ).build();
        }
        return noteDatabase;
    }
    public abstract NoteDAO noteDAO();
}

When I use List<Note> notes and notes.toString(), it only shows me the date and time, the title is null, I also notice that in the Dao interface, it raises 2 errors which are Cannot resolve symbol notes and Cannot resolve symbol id. I don't understand why it doesn't insert to the database. Can someone help me with this problem? Thanks for your help !

CodePudding user response:

The code you have included in your question is fine (after adding the getters and setters) and then using:-

public class MainActivity extends AppCompatActivity {

    NoteDatabase db;
    NoteDAO dao;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        db = NoteDatabase.getDatabase(this);
        dao = db.noteDAO();

        Note note1 = new Note();
        note1.setTitle("NOTE001");
        note1.setDatetime("2022-01-01 10:30");
        note1.setSubtitle("The First Note");
        note1.setWebsite("www.mynotes.com");
        note1.setNoteText("This is the note");
        note1.setColor("Black");
        note1.setImagePath("notes/note1.jpg");

        dao.insertNote(note1);
        for (Note n: dao.getAllNotes()) {
            Log.d("NOTEINFO","Title is "   n.getTitle()   " Date is "   n.getDatetime()   " blah blah ToString = "   n);
        }
    }
}
  • note the only amendment to the code copied from the question is the use of .allowMainThreadQueries in the databaseBuilder.

The result being:-

D/NOTEINFO: Title is NOTE001 Date is 2022-01-01 10:30 blah blah ToString = NOTE001 : 2022-01-01 10:30

However, adding :-

    Note note2 = new Note();
    //note1.setTitle("NOTE001"); //<<<<<<<<<<< OOOPS
    note2.setDatetime("2022-01-01 10:30");
    note2.setSubtitle("The Second Note");
    note2.setWebsite("www.mynotes.com");
    note2.setNoteText("This is the note");
    note2.setColor("Black");
    note2.setImagePath("notes/note2.jpg");

Results in what you appear to be describing as per:-

D/NOTEINFO: Title is null Date is 2022-01-01 10:30 blah blah ToString = null : 2022-01-01 10:30

I believe you saying

I also notice that in the Dao interface, it raises 2 errors which are Cannot resolve symbol notes and Cannot resolve symbol id

Is perhaps the clue to the cause, which could be that you have the incorrect dependencies you should have 2 for Room:-

  1. the runtime library e.g. implementation 'androidx.room:room-runtime:2.5.0-alpha02', and
  2. the compiler library e.g. annotationProcessor 'androidx.room:room-compiler:2.5.0-alpha02'

Another possibility is that in addition to the getters and setters you have a constructor that doesn't set correctly the title so it is left as null.

  • Related