Return type for Android Room joins

Dao

@Query("SELECT * FROM Foo")
List<FooAndBar> findAllFooAndBar();

Class FooAndBar

public class FooAndBar {
    @Embedded
    Foo foo;

    @Relation(parentColumn =  "Foo.bar_id", entityColumn = "Bar.id")
    List<Bar> bar;
    // If we are sure it returns only one entry
    // Bar bar;

    //Getter and setter...
}

This solution seems to work, but I’m not very proud of it.
What do you think about it?

Edit: Another solution

Dao, I prefer to explicitly select but “*” will do the job 🙂
Keep in mind that this solution only works when the fields of both entities are unique. See the comments for more information.

@Query("SELECT Foo.*, Bar.* FROM Foo INNER JOIN Bar ON Foo.bar = Bar.id")
List<FooAndBar> findAllFooAndBar();

Class FooAndBar

public class FooAndBar {
    @Embedded
    Foo foo;

    @Embedded
    Bar bar;

    //Getter and setter...
}

edit: since Version 2.2.0-alpha01, room @Relation annotation can manage One-To-One relation

Leave a Comment