Saving Data on Android


answer.kt  file under the  data



Download 19,28 Mb.
Pdf ko'rish
bet138/292
Sana24.01.2022
Hajmi19,28 Mb.
#407089
1   ...   134   135   136   137   138   139   140   141   ...   292
Bog'liq
Bailey J., Dominguez A., Djermanovic D. - Saving Data on Android (1st Edition) - 2019

answer.kt
 file under the 
data 

 model
 package.
The 
Answer
 entity already has a 
question_id
 field that you can use as a foreign key 
that points to the 
question_id
 field of your 
Question
 entity. The problem is that 
you still have not told Room that this is actually a foreign key. For this, you need to 
use the 
foreignKeys
 property of the 
@Entity
 annotation to define the 
relationships.
Saving Data on Android
Chapter 7: Mastering Relations
raywenderlich.com
125


Add a 
foreignKeys
 property to the 
@Entity
 annotation of your 
Answer
 class like 
this:
@Entity(tableName = "answer", 
    foreignKeys = [//1 
        ForeignKey(entity = Question::class,//2 
            parentColumns = ["question_id"],//3 
            childColumns = ["question_id"],//4 
            onDelete = CASCADE)
//5
 
    ])
Taking each commented section in turn:
1.
foreignKeys
 allows you to define a relationship between this and another entity. 
This property accepts an array of 
ForeignKey
 objects that you can use to define 
foreign key constraints.
2. The first parameter in the constructor of a 
ForeignKey
 object accepts the entity 
to which this entity is related. In this case, you are passing the 
Question
 class 
since you want to create a foreign key constraint to the 
Question
 entity.
3.
parentColumns
 accepts the column names in the parent entity as an array. Since 
you want to match each answer to a single question, you are going to pass the 
primary key of your Question entity: 
question_id
.
4.
childColumns
 accepts the column names in the current entity to use as foreign 
keys.
5.
onDelete
 tells Room what to do in case the parent entity is deleted from the 
database. For example, what would happen to your answers if the respective 
question is deleted?  When you enter 
CASCADE
 for the value of 
onDelete
, be sure 
to use the import for 
androidx.room.ForeignKey.CASCADE
.
There are several options for 
onDelete
:
1.
CASCADE
: If a record is deleted from the parent entity each row in the child 
entity that was associated with the parent entity is also deleted. For this app, you 
are using this option since you want to delete the answers if the question is 
deleted.
2.

Download 19,28 Mb.

Do'stlaringiz bilan baham:
1   ...   134   135   136   137   138   139   140   141   ...   292




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish