简体   繁体   中英

Room database INNER JOIN results

I have two tables with a relation one to one

For example :

Tables :

  • users [ u_id,u_name,u_address ]
  • address [ add_id,add_country,add_town,add_street ]

Which means two Entities and two DAO in my java code

When i want to execute this query : select * from users inner join address on u_address = add_id

It will return

LiveData<List<ItemUser>>

And like that i'll get u_address which is just an ID instead of ItemAddress object where i can get the data that i want.

I know that i can execute another method which can get the data of the selected address , but i want to know if it's possible to get the whole data by calling just one method ?

As I understand, you want the result of the relation of those two entities.

You can use the Relation Annotation

Same answer in stack overflow.

In your example would something like this.

Class

@Entity
public class Users {
 @PrimaryKey public final int u_id;
             public final String u_name;
             public final int u_address;
}

@Entity
public class Address {
@PrimaryKey public final int add_id;
            public final String add_country;
            public final String add_town;
            public final String add_street;
}

public class UserAndAddress{
@Embedded public Users user;

@Relation(parentColumn = "u_id",
          entityColumn = "add_id") public List<Address> addressList;
}

Dao

@Dao
public interface UserAndAddressDao {

@Query("SELECT * from users")
public List<UserAndAddress> getUsersAndAddress();
}

I have 2 similar tables

  1. Person
  2. Address

1. Person Table

@Entity(tableName = "person_table")
data class Person(
    @PrimaryKey
    var id:Int? = 0,
    var name:String? = "",
    @ColumnInfo(name = "email")
    var email:String? = ""    //EMAIL
)

2. Address Table

    @Entity(
    foreignKeys = [ForeignKey(
        entity = Person::class,
        parentColumns = arrayOf("id"),
        childColumns = arrayOf("personId"),
        onDelete = CASCADE
    )],
    tableName = "address_table"

)
data class Address(
    @PrimaryKey
    var id:Int? = 0,
    var name:String? = "" ,
    @ColumnInfo(name = "phoneNumber")
    var phoneNo:String? = "" ,    //PHONE NO
    var personId:Int? = 0
)

And I want to write an inner join query to get email from person and Phone number from Address table

So I made a new Table

data class PersonAddress(
    @ColumnInfo(name = "PhNumber") var phoneNo: String? = "" ,
    @ColumnInfo(name = "Email") var email: String? = ""
)

and write this query in DAO

@Dao
interface AddressDAO {

    @Query("SELECT address_table.phoneNumber as PhNumber, person_table.email as"  
           + "Email FROM address_table" +
           "INNER JOIN person_table ON person_table.id = address_table.personId" 
           + "WHERE person_table.id =:personId")
    fun findAddressesForPerson(personId: Int): LiveData<List<PersonAddress>>

}

and I get the result succesfully :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM