添加1对多关系android房间数据库?



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

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

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

我也有书籍和计数的视图模型,我用它来填充一个回收器视图。然而,当我尝试查看回收器视图时,我得到了错误。

[Update]我找到问题了。这是很多事情的结合。首先,我没有注释处理器依赖。这些是你需要的依赖项。

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

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

相关内容

  • 没有找到相关文章

最新更新