How to get related data using Room - android

#Entity
data class User(
#PrimaryKey
val id: Int,
val petsId: List<String>
)
#Entity
data class Pet(
#PrimaryKey
val id: Int,
val name: String
)
How to get UserWithPets from the two User and Pet entities using Room?
data class UserWithPets(
val id: Int,
val pets: List<Pet>
)

Related

Android Room: Entities and POJOs must have a usable public constructor

I'm trying to save the data class i got from json to room database and suddenly get errors like this while building the app.
Entities and POJOs must have a usable public constructor. You can have an empty constructor or a constructor whose parameters match the fields (by name and type).
NewsResponse model object
data class NewsResponse(
val copyright: String,
val response: Response,
val status: String
)
Response object
data class Response(
val docs: List<Docs>
)
Docs object(this is the data class i want to save)
#Entity(
tableName = "docs"
)
data class Docs(
#PrimaryKey(autoGenerate = true)
var id: Int? = null,
val _id: String,
val `abstract`: String,
#Embedded
val byline: Byline,
val document_type: String,
#Embedded
val multimedia: Multimedia,
#Embedded
val headline: Headline,
val lead_paragraph: String,
val news_desk: String,
val pub_date: String,
val section_name: String,
val snippet: String,
val source: String,
val subsection_name: String,
val type_of_material: String,
val uri: String,
val web_url: String,
val word_count: Int
)
Byline object
#Entity
data class Byline(
val original: String
)
Multimedia object
#Entity
data class Multimedia(
val url: String
)
Headline object
#Entity
data class Headline(
val main: String
)
Any ideas how to solve this?

Cant figure out return type of room db

My Entity
#Entity(tableName = "my_entity_table")
internal data class MYEntity(
#PrimaryKey(autoGenerate = true)
val pcID: Int,
val server_id: String,
val name: String,
val detail: String,
val duration: Int,
val start_date : Long,
val end_date: Long,
val created_by_user_id: String,
val db_create_dttm: Long,
val db_update_dttm: Long,
val db_create_src: Long,
val db_update_src: Long
)
My dao query where I to select the duration,start_date and end_date
#Query("select duration,start_date,end_date from my_entity_table where id =:pcID")
suspend fun getDurationDetails(pcID:Int) : What should be the return type?
What should be the return type of the query ??
You could make a specific data class as model for your query with the specific fields:
data class ExampleModel(
#ColumnInfo(name = "duration") val duration: Int?,
#ColumnInfo(name = "start_date") val start_date: Long?
#ColumnInfo(name = "end_date") val end_date: Long?
)
And then use it as return:
#Query("select duration,start_date,end_date from my_entity_table where id =:pcID")
suspend fun getDurationDetails(pcID:Int) : ExampleModel
I don't think that using the Entity model is posible cause your Entity parameters are not nullable and you query just a few of that parameters.

Room database relation not applying

I can't make the relation between these entities.
with SQL query I can make relation but with room not working.
I think this a room bug but don't know why. and taking me a lot of time.
using 2.2.6 version.
#Entity
data class Movie(
#SerializedName("country")
val country: String,
#SerializedName("genres")
val genres: List<String>,
#SerializedName("id")
#PrimaryKey(autoGenerate = false)
val id: Int,
#SerializedName("images")
val images: List<String>? = null,
#SerializedName("imdb_rating")
val imdbRating: String,
#SerializedName("poster")
val poster: String,
#SerializedName("title")
val title: String,
#SerializedName("year")
val year: String
)
#Entity
data class FavoriteMovie(
#PrimaryKey(autoGenerate = false)
val id: Int ,
val movieId: Int,
val createTime:String
)
data class MovieAndFavoriteMovie(
#Embedded val movie: Movie,
#Relation(parentColumn = "id", entityColumn = "movieId")
val favoriteMovie: FavoriteMovie
)
DAO
#Query("SELECT * FROM Movie")
#Transaction
fun getAllFavoriteMovies(): Flow<List<MovieAndFavoriteMovie>>
Error log
java.lang.NullPointerException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkNotNullParameter, parameter favoriteMovie
at com.movie_explorer.data.database.MovieAndFavoriteMovie.<init>(Unknown Source:7)
at com.movie_explorer.data.database.dao.FavoriteMovieDao_Impl$5.call(FavoriteMovieDao_Impl.java:166)
at com.movie_explorer.data.database.dao.FavoriteMovieDao_Impl$5.call(FavoriteMovieDao_Impl.java:112)
at androidx.room.CoroutinesRoom$Companion$createFlow$1$1.invokeSuspend(CoroutinesRoom.kt:81)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)

Insert Nested data classes into Room db

I'm in situation where i have other model classes nested inside a parent class , and the thing is that i want to insert the model classes into room db but it is asking me to use type converter but i don't know to and from which type should i convert the data class , this is my code
data class FixturesMergedModels (
#Embedded var event: Event,
#Embedded var teamXX: TeamXX,
#Embedded var awaylogo: testAwayLogoModel
)
My Event Model
#Entity
data class Event(
#PrimaryKey(autoGenerate = true)
var evenid : Int?,
#SerializedName("dateEvent")
var dateEvent : String?,
#SerializedName("strTime")
val strTime : String?,
#SerializedName("strThumb")
val strThumb : String?,
#SerializedName("strHomeTeam") //
val strHomeTeam : String?,
#SerializedName("strAwayTeam") //
val strAwayTeam : String?,
#SerializedName("idAwayTeam")
val idAwayTeam: Int?,
#SerializedName("idEvent")
val idEvent: Int?,
#SerializedName("idHomeTeam")
val idHomeTeam : Int?,
#SerializedName("intHomeScore") //
val intHomeScore : String?,
#SerializedName("intAwayScore") //
val intAwayScore : String?
)```
* Home Team Model ( teamxx)
#Entity
data class TeamXX(
#PrimaryKey(autoGenerate = true)
var homeTeamID : Int?,
#SerializedName("strTeamBadge")
val HomestrTeamBadge: String?
)```
Away Team Model
data class testAwayLogoModel (
#PrimaryKey(autoGenerate = true)
var awayTeamID : Int?,
#SerializedName("strTeamBadge")
val AwaystrTeamBadge: String?
)
According to the google developer website, you should use #Embedded for your event and teamXX class. Here is a short example given at their website.
data class Address(
val street: String?,
val state: String?,
val city: String?,
#ColumnInfo(name = "post_code") val postCode: Int)
#Entity
data class User(
#PrimaryKey val id: Int,
val firstName: String?,
#Embedded val address: Address?
)
for more detail you can visit here: https://developer.android.com/training/data-storage/room/relationships

Data Class and properties from other Data Class

I'm downloading few lists (RawAlbum, RawPhoto) from the web and I want to use properties from them to create another list. Then I want to use it to make a recyclerview.
So that's how it looks. It is the ListItem:
#Parcelize
data class ListItem(
val id: Int,
val title: String,
val albumTitle: String,
val thumbnailUrl: String
) : Parcelable
So I want to use the id and title from the RawAlbum. While the albumTitle and thumbnailUrl from the RawPhoto.
RawAlbum:
#Parcelize
#Entity(tableName = "albums")
data class RawAlbum(
#PrimaryKey
val id: Int,
val userId: Int,
val title: String
) : Parcelable
RawPhoto:
#Parcelize
#Entity(tableName = "photos")
data class RawPhoto(
#PrimaryKey
val id: Int,
val albumId: Int,
val title: String,
val url: String,
val thumbnailUrl: String
) : Parcelable

Categories

Resources