Reputation: 5291
I have implementd Room persistence for store the notification but it's throw exception with below log,for the builde
implementation for modules.room_db.AppDatabase. AppDatabase_Impl does not exist
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.RuntimeException: cannot find implementation for modules.room_db.AppDatabase. AppDatabase_Impl does not exist
at android.arch.persistence.room.Room.getGeneratedImplementation(Room.java:92)
at android.arch.persistence.room.RoomDatabase$Builder.build(RoomDatabase.java:454)
at modules.room_db.AppDatabase.getAppDatabase(AppDatabase.java:23)
at MypackageName.SplashActivity.onCreate(SplashActivity.java:46)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
Store notification
AppDatabase.getAppDatabase(getApplicationContext()).userDao().insertNotification(notificationDetail);
Gralde
implementation 'android.arch.persistence.room:runtime:1.0.0'
annotationProcessor 'android.arch.persistence.room:compiler:1.0.0'
androidTestImplementation 'android.arch.persistence.room:testing:1.0.0'
AppDatabase.java
public abstract class AppDatabase extends RoomDatabase {
private static final String DATABASE_NAME = "notification_room_db";
public abstract NotificationDao userDao();
private static AppDatabase INSTANCE;
public static AppDatabase getAppDatabase(Context context) {
if (INSTANCE == null) {
INSTANCE =
Room.databaseBuilder(context.getApplicationContext(), AppDatabase.class, DATABASE_NAME)
// allow queries on the main thread.
// Don't do this on a real app! See PersistenceBasicSample for an example.
.allowMainThreadQueries()
.build();
}
return INSTANCE;
}
public static void destroyInstance() {
INSTANCE = null;
}
}
NotificationDao.java
public interface NotificationDao {
@Query("SELECT * FROM notifications")
List<NotificationDetail> getAll();
@Query("SELECT * FROM notifications WHERE notification_id IN (:userIds)")
List<NotificationDetail> loadAllByIds(int[] userIds);
@Insert
void insertNotification(NotificationDetail... users);
@Update
void updateNotification(NotificationDetail notificationDetail);
@Delete
void delete(NotificationDetail user);
}
NotificationDetail.java
@Entity(tableName = "notifications")
public class NotificationDetail {
@PrimaryKey(autoGenerate = true)
private int notification_id;
@ColumnInfo(name = "message")
private String message;
@ColumnInfo(name = "title")
private String title;
@ColumnInfo(name = "type")
private String type;
@ColumnInfo(name = "order_id")
private int order_id;
@ColumnInfo(name = "is_displayed")
private boolean isSeen;
}
I have already search for this,and there is same question for kotlin i am looking for the answer in android.
Upvotes: 4
Views: 11537
Reputation: 5291
When we are working with Room persistence
it's good to define proper annotation for each class.
1) Define your Entity
class with annotation @Entity
like below
@Entity(tableName = "notifications")
public class NotificationDetail {
@PrimaryKey(autoGenerate = true)
private int notification_id;
@ColumnInfo(name = "message")
private String message;
@ColumnInfo(name = "title")
private String title;
}
2) Define your Dao interface with @Dao
annotation,like below
@Dao
public interface NotificationDao {
@Query("SELECT * FROM notifications")
List<NotificationDetail> getAll();
@Insert
void insertNotification(NotificationDetail... users);
@Update
void updateNotification(NotificationDetail notificationDetail);
@Delete
void delete(NotificationDetail user);
}
3) Database class that extends RoomDatabase
define the @Database
annotation with all the Entity class,like below
@Database(entities = {NotificationDetail.class}, version = 1, exportSchema = false)
public abstract class AppDatabase extends RoomDatabase {
private static final String DATABASE_NAME = "notification_room_db";
public abstract NotificationDao userDao();
private static AppDatabase INSTANCE;
public static AppDatabase getAppDatabase(Context context) {
if (INSTANCE == null) {
INSTANCE =
Room.databaseBuilder(context.getApplicationContext(), AppDatabase.class, DATABASE_NAME)
// allow queries on the main thread.
// Don't do this on a real app! See PersistenceBasicSample for an example.
.allowMainThreadQueries()
.build();
}
return INSTANCE;
}
public static void destroyInstance() {
INSTANCE = null;
}
}
I have forgot to add @Database annotation in database class
Upvotes: 5