添加1到多关系android Room Database?

Posted

技术标签:

【中文标题】添加1到多关系android Room Database?【英文标题】:Adding a 1 to Many relationship android Room Database? 【发布时间】:2021-10-10 23:22:22 【问题描述】:

我很难弄清楚如何在我的数据库中添加一对多关系。 Book 实体需要保存许多计数。当我尝试它时,我收到一条错误消息:

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)

这是我的 AppDatabase 的样子:

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();


计数实体:

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 +
                '';
    


图书实体:

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 实体

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;
    



计数道

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;
    



书道


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);


我还有用于填充回收站视图的 Books 和 Counts 的视图模型。但是,当我尝试查看回收站视图时出现错误。

【问题讨论】:

您的异常似乎是关于构建而不是代码。见***.com/questions/46665621/… 好的,看来我添加的 Count 和 Book 的关系正确吗? 【参考方案1】:

[更新] 我发现了这个问题。这是一个组合。首先,我没有注释处理器依赖项。这些是您需要的依赖项。

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

我在使用没有日期转换器的 Date 类型时也遇到了问题。我不想弄乱日期转换器。所以我只是将类型更改为字符串。在我能够让它工作之后。

【讨论】:

以上是关于添加1到多关系android Room Database?的主要内容,如果未能解决你的问题,请参考以下文章

Android Room - 带有附加字段的多对多关系

如何在 Access 中添加具有其他多数据(1 到多)关系的记录

Android Room数据库使用

Android Room数据库使用

Android Room - 处理对象中的对象列表和查询结果

保存到多对多关系