Kman
Kman

Reputation: 135

Adding a 1 to Many relationship android Room Database?

I am having a hard time trying to figure out how to add a one to many relationship in my database. The Book entity needs to hold many Counts. When I tried it, I got an error that says:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.counter, PID: 11949
java.lang.RuntimeException: cannot find implementation for com.example.counter.db.AppDatabase. AppDatabase_Impl does not exist
    at androidx.room.Room.getGeneratedImplementation(Room.java:97)
    at androidx.room.RoomDatabase$Builder.build(RoomDatabase.java:1358)
    at com.example.counter.db.AppDatabase.getInstance(AppDatabase.java:18)

Here is what my AppDatabase Looks like:

package com.example.counter.db;

import android.content.Context;

import androidx.room.Database;
import androidx.room.Room;
import androidx.room.RoomDatabase;

@Database(entities = {Count.class, Book.class}, version = 2, exportSchema = false)
public abstract class AppDatabase extends RoomDatabase {

    private static AppDatabase instance;

    public static AppDatabase getInstance(Context context){
        if(instance != null){
            return instance;
        }else{
            instance = Room.databaseBuilder(context, AppDatabase.class, "AppDatabase_database")
                    .build();
            return instance;
        }
    }

    public abstract CountDAO countDAO();
    public abstract BookDAO bookDAO();
}

Count Entity:

package com.example.counter.db;

import androidx.room.Embedded;
import androidx.room.Entity;
import androidx.room.ForeignKey;
import androidx.room.PrimaryKey;

import java.text.SimpleDateFormat;
import java.util.Date;

import static androidx.room.ForeignKey.CASCADE;

@Entity
public class Count {


    @PrimaryKey(autoGenerate = true)
    private long count_id;

    private long book_id;

    private String title;
    private Date date;
    private int total;

    public Count( long book_id, String title, Date date, int total) {
        this.book_id = book_id;
        this.title = title;
        this.date = date;
        this.total = total;
    }


    public long getCount_id() {
        return count_id;
    }

    public void setCount_id(long id) {
        this.count_id = id;
    }

    public long getBook_id() {
        return book_id;
    }

    public void setBook_id(long book_id) {
        this.book_id = book_id;
    }

    public String getCountTitle() {
        return title;
    }

    public void setCountTitle(String title) {
        this.title = title;
    }

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public int getTotal() {
        return total;
    }

    public void setTotal(int total) {
        this.total = total;
    }

    @Override
    public String toString() {
        return "Count{" +
                "id=" + count_id +
                ", book_id=" + book_id +
                ", title='" + title + '\'' +
                ", date=" + date +
                ", total=" + total +
                '}';
    }
}

Book Entity:

package com.example.counter.db;

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

import java.util.Date;


@Entity
public class Book {

    @PrimaryKey(autoGenerate = true)
    private long book_id;

    private String title;
    private Date date;


    public Book(String title, Date date) {
        this.title = title;
        this.date = date;
    }

    public long getBook_id() {
        return book_id;
    }

    public void setBook_id(long id) {
        this.book_id = id;
    }

    public String getBookTitle() {
        return title;
    }

    public void setBookTitle(String title) {
        this.title = title;
    }

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }


    @Override
    public String toString() {
        return "Book{" +
                "id=" + book_id +
                ", title=" + title +
                ", date=" + date +
                '}';
    }
}

BookWithCounts Entity

package com.example.counter.db;

import androidx.room.Embedded;
import androidx.room.Relation;

import java.util.List;

public class BookWithCounts {
    @Embedded
    public Book book;
    @Relation(
            parentColumn = "book_id",
            entityColumn = "book_id"
    )

    public List<Count> counts;

    public BookWithCounts(Book book, List<Count> counts){
        this.book = book;
        this.counts = counts;
    }

}

CountDAO

package com.example.counter.db;

import androidx.room.Embedded;
import androidx.room.Relation;

import java.util.List;

public class BookWithCounts {
    @Embedded
    public Book book;
    @Relation(
            parentColumn = "book_id",
            entityColumn = "book_id"
    )

    public List<Count> counts;

    public BookWithCounts(Book book, List<Count> counts){
        this.book = book;
        this.counts = counts;
    }

}

BookDAO


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

import java.util.List;

@Dao
public interface BookDAO {

    @Query("select * from Book")
    LiveData<List<Book>> getAllBooks();

    @Query("select * from Book where book_id=:id")
    List<Book> getBookByID(long id);

    @Query("select title from Book")
    LiveData<List<String>> getAllBookTitles();

    @Insert
    long insertBook(Book book);

    @Insert
    long insertCount(Count count);

    @Update
    void updateBook(Book book);

    @Delete
    void deleteBook(Book book);

}


BooksWithCountsDAO

package com.example.counter.db;

import androidx.room.Dao;
import androidx.room.Insert;
import androidx.room.Transaction;

import java.util.List;

@Dao
public interface BookWithCountsDAO {
    @Transaction
    @Insert
    long insertBook(Book book);

    @Insert
    void insertCounts(List<Count> counts);
}

I also have View Models for both Books and Counts that I use to fill a recycler view. However, I get the error when I try and view the recycler view.

Upvotes: 2

Views: 408

Answers (1)

Kman
Kman

Reputation: 135

[Update] I figured out the issue. It was a combination of things. First I didn't have the annotation processor dependency. These are the dependencies you need.

    def room_version = "2.3.0"
    implementation "androidx.room:room-runtime:$room_version"
    annotationProcessor "androidx.room:room-compiler:$room_version"

I also had issues with using a type Date with no date converter. I didn't want to mess with the date converter. So I simply just changed the type to a string. After I was able to get it working.

Upvotes: 0

Related Questions