I have created a relation between User, Property and junction table of these two items UserPropertyJunction which can be visualized as releation in the image below
UserEntity, instead of customers
, which is
@Entity(tableName = "user")
data class UserEntity(
@PrimaryKey
val userId: Long,
val firstName: String,
val lastName: String,
val email: String,
val password: String
)
Property, instead of products
,
@Entity(
tableName = "property",
primaryKeys = ["id"]
)
data class Property(
val id: Int,
val update: Int,
val categoryId: Int,
val title: String,
val subject: String,
val type: String,
val typeId: Int
}
And Junction table instead of product_helper
@Entity(
tableName = "user_property_junction",
primaryKeys = ["userAccountId", "propertyId"],
// Foreign Keys
foreignKeys = [
ForeignKey(
entity = User::class,
parentColumns = ["userId"],
childColumns = ["userAccountId"],
onDelete = ForeignKey.NO_ACTION
),
ForeignKey(
entity = Property::class,
parentColumns = ["id"],
childColumns = ["propertyId"],
onDelete = ForeignKey.NO_ACTION
)
]
)
data class UserPropertyJunction(
val userAccountId: Long,
val propertyId: Int
)
And created relation class with
data class UserWithFavorites(
@Embedded
val user: User,
@Relation(
parentColumn = "userId",
entity = Property::class,
entityColumn = "id",
associateBy = Junction(
value = UserPropertyJunction::class,
parentColumn = "userAccountId",
entityColumn = "propertyId"
)
)
val propertyList: List<Property>
)
Also need to get data of how many times these properties displayed and liked by users.
And for that checked out the solution in this link which adds additional field to junction, in my case adding displayCount
and favorite
properties
data class UserPropertyJunction(
val userAccountId: Long,
val propertyId: Int,
val displayCount:Int=0,
val favorite:Boolean=false
)
My first question is as far as i have seen, not much experience with junction or associative tables, they only store foreign keys for the tables that should associate with, is it okay to add value fields to junction table?
If it's not elegant or not preferred way of doing it, should i add another table that has relation with junction table such as
data class PropertyStatus(
val userAccountId: Long,
val propertyId: Int,
val displayCount:Int=0,
val favorite:Boolean=false
)
and associate them?
And when retrieving data from both properties of a User and status of properties should i manually get data from
data class UserWithProperties(
@Embedded
val user: User,
@Relation(
parentColumn = "userId",
entity = Property::class,
entityColumn = "id",
associateBy = Junction(
value = UserPropertyJunction::class,
parentColumn = "userAccountId",
entityColumn = "propertyId"
)
)
val propertyList: List<Property>
)
and get with SELECT from status table or junction table based on your answer to first question
or is it possible to add another relation to UserWithProperties
with @Embedded
or @Relation
UserAndLibrary
properties withSELECT
andJOIN
? I also don't even need user, for these query only need the property and how many times it's displayed and whether it's favorited by the user withuserAccountId
– Yacht