Created
June 6, 2020 15:07
-
-
Save mwshubham/8bb42465b7bf3efb1567558631ea12f0 to your computer and use it in GitHub Desktop.
Room one to many relation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Entity(tableName = Company.Companion.TableInfo.tableName) | |
data class Company( | |
@PrimaryKey | |
var _id: String = "", | |
var company: String = "", | |
var website: String = "", | |
var logo: String = "", | |
var about: String = "", | |
@Ignore | |
var members: List<Member> = emptyList() | |
) { | |
companion object { | |
object TableInfo { | |
const val tableName = "companies" | |
} | |
} | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Dao | |
interface CompanyDao { | |
@Insert(onConflict = OnConflictStrategy.REPLACE) | |
fun insert(companies: List<Company>): List<Long> | |
// @Query("DELETE FROM companies") | |
// fun deleteAll() | |
// | |
// @Transaction | |
// @Query("SELECT * FROM companies where _id =:id") | |
// fun get(id: String): LiveData<Company> | |
// | |
@Transaction | |
@Query("SELECT * FROM companies") | |
fun getAll(): Flow<List<CompanyMemberRelation>> | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
data class CompanyMemberRelation( | |
@Embedded val company: Company, | |
@Relation( | |
parentColumn = "_id", | |
entityColumn = "companyId" | |
) | |
val members: List<Member> | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Entity(tableName = Member.Companion.TableInfo.tableName) | |
data class Member( | |
@PrimaryKey | |
var _id: String = "", | |
var companyId: String = "", | |
var age: String = "", | |
@Embedded | |
var name: Name = Name(), | |
var email: String = "", | |
var phone: String = "" | |
) { | |
companion object { | |
object TableInfo { | |
const val tableName = "members" | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Dao | |
interface MemberDao { | |
@Insert(onConflict = OnConflictStrategy.IGNORE) | |
fun insert(members: List<Member>): List<Long> | |
@Query("DELETE FROM members") | |
fun deleteAll() | |
@Query("SELECT * FROM members where _id =:id") | |
fun get(id: String): LiveData<Member> | |
@Query("SELECT * FROM members") | |
fun getAll(): Flow<List<Member>> | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment