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 Book
s 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