Not able to get query results using Room persistence

前端 未结 2 1474
既然无缘
既然无缘 2021-02-08 03:17

I am developing an Android app using the Room persistence library. I have a User and a Car entity

@Entity(tableName = \"users\")
public class User {

    @Primar         


        
相关标签:
2条回答
  • 2021-02-08 03:58

    Change your query:

    @Query("SELECT * FROM users JOIN cars ON users.id = cars.userId")
    

    to specify the columns in the POJO class UserWithCar. The columns returned must match all the columns, and have the same column name as your POJO. You can use AS to change the column names in the query.

    @Query("SELECT userId as userId , brand as brand FROM users JOIN cars ON users.id = cars.userId")
    

    Alternatively, you can use @ColumnInfo to specify the column name mappings.

    0 讨论(0)
  • 2021-02-08 04:13

    Columns returned by the query: id, name, id, userId, brand. Fields in com.foodtec.roomdemo.data.models.UserWithCar: user_id, user_name, car_id, car_userId, car_brand.

    Error indicates that, columns returned by query is different from Pojo class. It should be the same. Alternatively you can map your Pojo variable to column name using @ColumnInfo annotation.

    For example,

    @PrimaryKey
    @NonNull
    @ColumnInfo(name = "user_id")
    private int id;
    

    This way, id will be mapped to user_id.

    0 讨论(0)
提交回复
热议问题