1

When I made a method that select all the items from a table name the Dao class could not resolve the table name!!

I could not understand why this happened at all. The problem is that I had another project that I was working on (two days ago!!) in which I used Room as well and it was working perfectly, and when I made this new project and defined the classes shown below the old project has the same problem now! (lol)

my object class

package com.example.adapterapp.Entites;

import androidx.room.Entity;
import androidx.room.Index;
import androidx.room.PrimaryKey;

@Entity(tableName = "items_table")
public class RoomItem {

    @PrimaryKey(autoGenerate = true)
    private int itemId;

    private final String itemTitle;

    private final String itemSubtitle;

    private final String imageUri;

    public RoomItem(String itemTitle, String itemSubtitle, String imageUri) {
        this.itemTitle = itemTitle;
        this.itemSubtitle = itemSubtitle;
        this.imageUri = imageUri;
    }

    public int getItemId() {
        return itemId;
    }

    public String getItemTitle() {
        return itemTitle;
    }

    public String getItemSubtitle() {
        return itemSubtitle;
    }

    public String getImageUri() {
        return imageUri;
    }


}

dao

package com.example.adapterapp.RoomDatabase;

import androidx.lifecycle.LiveData;
import androidx.room.Dao;
import androidx.room.Delete;
import androidx.room.Insert;
import androidx.room.Query;
import androidx.room.Update;

import com.example.adapterapp.Entites.RoomItem;

import java.util.List;

@Dao
public interface ItemDao {

    @Insert
    void insertItem(RoomItem roomItem);

    @Update
    void updateItem(RoomItem roomItem);

    @Delete
    void deleteItem(RoomItem roomItem);

    @Query("SELECT * FROM items_table")
    LiveData<List<RoomItem>> getAllItems();

}

database

package com.example.adapterapp.RoomDatabase;

import android.content.Context;

import androidx.annotation.NonNull;
import androidx.room.Database;
import androidx.room.Room;
import androidx.room.RoomDatabase;
import androidx.sqlite.db.SupportSQLiteDatabase;

import com.example.adapterapp.Entites.RoomItem;

import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

@Database(entities = RoomItem.class, version = 1)
public abstract class ItemDatabase extends RoomDatabase {

    private static ItemDatabase instance;

    public abstract ItemDao noteDao();

    public static synchronized ItemDatabase getInstance(Context context) {
        if (instance == null) {
            instance = Room.databaseBuilder(context.getApplicationContext(),
                    ItemDatabase.class, "items_database")
                    .fallbackToDestructiveMigration()
                    .addCallback(roomCallback)
                    .build();
        }
        return instance;
    }

    private static final RoomDatabase.Callback roomCallback = new RoomDatabase.Callback() {
        @Override
        public void onCreate(@NonNull SupportSQLiteDatabase db) {
            super.onCreate(db);
            new PopulateTaskRunner().executeAsync(new PopulateDatabaseTask(instance));
        }
    };

    private static class PopulateTaskRunner {
        private final Executor executor = Executors.newSingleThreadExecutor(); // change according to your requirements

        private void executeAsync(PopulateDatabaseTask populateDatabaseTask) {
            executor.execute(() -> {
                try {
                    populateDatabaseTask.itemDao.insertItem(new RoomItem("title: 1", "subtitle: 1", null));
                    populateDatabaseTask.itemDao.insertItem(new RoomItem("title: 2", "subtitle: 2", null));
                    populateDatabaseTask.itemDao.insertItem(new RoomItem("title: 3", "subtitle: 3", null));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });
        }
    }

    private static class PopulateDatabaseTask {

        private final ItemDao itemDao;

        private PopulateDatabaseTask(ItemDatabase itemDatabase) {
            this.itemDao = itemDatabase.noteDao();
        }
    }

}

dependencies {

    implementation 'androidx.appcompat:appcompat:1.4.2'
    implementation 'com.google.android.material:material:1.6.1'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'

    //lifecycle
    def lifecycle_version = "2.6.0-alpha01"
    implementation "androidx.lifecycle:lifecycle-viewmodel:2.5.0"
    implementation "androidx.lifecycle:lifecycle-livedata:2.5.0"
    implementation "androidx.lifecycle:lifecycle-common-java8:$lifecycle_version"

    //room database
    def room_version = "2.4.2"
    implementation "androidx.room:room-runtime:$room_version"
    annotationProcessor "androidx.room:room-compiler:2.4.2"

    //firebase
    // Import the BoM for the Firebase platform
    // When using the BoM, you don't specify versions in Firebase library dependencies
    implementation platform('com.google.firebase:firebase-bom:30.2.0')
    //implementation 'com.google.firebase:firebase-analytics'
    implementation 'com.google.firebase:firebase-firestore'
    implementation 'com.google.firebase:firebase-storage'
    implementation 'com.google.firebase:firebase-messaging'
    implementation 'com.google.firebase:firebase-auth'
}

enter image description here

1
  • Did you try to Rebuild Project? Commented Jul 20, 2022 at 16:20

1 Answer 1

5

I had the same problem recently and found out that I just forgot to put this {}

Change

@Database(entities = RoomItem.class, version = 1)

To

@Database(entities = {RoomItem.class}, version = 1)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.