Java android room get java objects from two tables

kiki Kala2 :

I have a two tables and I want to get one Java objects.

This is my table one:

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

and this is my table two :

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

And id from table one is this same that id table 3

and I want to get query in which I have a this class :

long id;
String date;
int azimuth;
String name;
String symbol;
String type;
Prashant Patel :

First, create a data class say ResultTable that contains the fields you want:

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

Now you can query the data from two tables and get the result into the above table, as below;

@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)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=155916&siteId=1