Android Room: How to read from two tables at the same time - duplicate id column

后端 未结 3 1899

I\'m new to Android Room. I want to read from a table and also read from a related table. The relationship is pretty common. One table defines instances. The other table defines

3条回答
  •  既然无缘
    2021-02-07 09:36

    waqaslam pointed the way to the solution. He suggested to alias the column names. The additional step is to give the second table a prefix.

    public class AnimalWithType {
    @Embedded
    private Animal animal;
    
    @Embedded(prefix = "type_")
    private AnimalType type;
    ...
    

    The column aliases are long and messy. I hope that someone can find a cleaner way.

    @Query("select animal.id, animal.name..., animal_type.id as type_id... from animal join animal_type on (animal.id = animal_type.id)")
    

提交回复
热议问题