Pro Android with Kotlin


Download 5.42 Mb.
Pdf ko'rish
bet106/223
Sana04.11.2023
Hajmi5.42 Mb.
#1746807
1   ...   102   103   104   105   106   107   108   109   ...   223
Bog'liq
@de android telegram Pro Android with Kotlin Developing Modern Mobile

119
CHAPTER 8: APIs
Here are a few notes about this construct:

In Java you would write @Entity(foreignKeys = @ForeignKey( ... ) . 
Kotlin doesn’t allow annotations inside annotations. In this case, using 
the constructor serves as a substitute, which boils down to omitting the 
@ for inner annotations.

In a Java annotation, attribute value arrays are written like name = { 
..., ... }. This cannot be used in Kotlin because the curly braces do 
not serve as array initializers. Instead, the arrayOf(...) library method 
gets used.

The childColumns attribute points to the reference key in this entity, 
Contact.employeeId in this case.

The parentColumns attribute points to the referred-to foreign key entity, 
in this case Employee.uid.

The onDelete attribute tells what to do if the parent gets deleted. A value 
of ForeignKey.CASCADE means to also automatically remove all children
which is the associated Contact entities. The possible values are as 
follows:

CASCADE: Transport all actions to the root of the child-parent relation 
tree.

NO_ACTION: Don’t do anything. This is the default, and it leads to an 
exception if the relationship breaks because of update or delete 
actions.

RESTRICT: Similar to NO_ACTION, but the check will be made 
immediately when a delete or an update happens.

SET_NULL: All child key columns get set to null if a parent delete or 
update happens.

SET_DEFAULT: All child key columns get set to their default if a parent 
delete or update happens.

The onUpdate attribute tells what to do if the parent gets updated. 
A value ForeignKey.CASCADE means to also automatically update all 
children, which are the associated Contact entities. The possible values 
are the same as for onDelete.

The deferred = true setting will postpone the consistency check 
until the database transaction is committed. This might, for example, 
be important if both parent and child get created inside the same 
transaction.

Foreign keys must be part of a corresponding index. Here Contact.
employeeId gets the index. You’ll learn more about indexes in the 
following sections.



Download 5.42 Mb.

Do'stlaringiz bilan baham:
1   ...   102   103   104   105   106   107   108   109   ...   223




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