Return type for Android Room joins

前端 未结 5 1095
谎友^
谎友^ 2020-11-28 23:31

Let\'s say I want to do an INNER JOIN between two entities Foo and Bar:

@Query(\"SELECT * FROM Foo INNER JOIN Bar ON F         


        
5条回答
  •  北海茫月
    2020-11-29 00:11

    Dao

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

    Class FooAndBar

    public class FooAndBar {
        @Embedded
        Foo foo;
    
        @Relation(parentColumn =  "Foo.bar_id", entityColumn = "Bar.id")
        List 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 :)

    @Query("SELECT Foo.*, Bar.* FROM Foo INNER JOIN Bar ON Foo.bar = Bar.id")
    List 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

提交回复
热议问题