Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have two entities similar to these

@Entity
class Book {
    @PrimaryKey
    String _id;
    String author_id;
    String title;
    //other fields
    //getter setter
}

@Entity
class Author {
    @PrimaryKey
    String _id;
    String name;
    //other fields
    //getter setter
}

I want to query a list of Books by a condition other than author, then with each book, I want to get and use the associated Author, for example book.getAuthor().getName(). Relations like one-to-one, one-to-many and many-to-many are not what I wanted. Finally, I found this Android doc, Referencing complex data using Room. It perfectly described my usecase, but it also said:

Room disallows object references between entity classes. Instead, you must explicitly request the data that your app needs.

To reference multiple entities at the same time using Room, you instead create a POJO that contains each entity, then write a query that joins the corresponding tables. This well-structured model, combined with Room's robust query validation capabilities, allows your app to consume fewer resources when loading data, improving your app's performance and user experience.

I have been searching around and I can't find any example for such POJO. Can someone give me an example? Thanks in advance.

question from:https://stackoverflow.com/questions/65559568/android-room-database-object-references

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
454 views
Welcome To Ask or Share your Answers For Others

1 Answer

Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...