Java Android房间从两个表中获取Java对象

时间:2019-06-14 07:49:00

标签: java android android-room

我有两个表,我想得到一个java对象:

这是我的表一:

@PrimaryKey(autoGenerate = false)
@ColumnInfo(name = "id")
long id;
@ColumnInfo(name = "date")
String date;
@ColumnInfo(name = "azimuth")
int azimuth;

这是我的第二张表:

   @PrimaryKey(autoGenerate = false)
    @ColumnInfo(name = "id")
    long id;
    @ColumnInfo(name = "name")
    String name;
    @ColumnInfo(name = "symbol")
    String symbol;
    @ColumnInfo(name = "type")
    String type;

表1中的ID与表3中的ID相同

我想了解一下,我有一个此类:

long id;
String date;
int azimuth;
String name;
String symbol;
String type;

1 个答案:

答案 0 :(得分:1)

首先,创建一个数据类ResultTable,其中包含您想要的字段:

public class ResultTable{
    long id;
    String date;
    int azimuth;
    String name;
    String symbol;
    String type;
}

现在,您可以从两个表中查询数据,并将结果放入上表,如下所示;

@Query("SELECT id, t1.date as date, t1.azimuth as azimuth, t2.name as name, t2.symbol as symbol, t2.type as type FROM tabel1 t1, table2 t2 WHERE t1.id = t2.id AND t1.id = :id")
List<ResultTable> performJoinQuery(long id)