Home » Android » java – Using Room's @ForeignKey as @Entity parameter in Kotlin

java – Using Room's @ForeignKey as @Entity parameter in Kotlin

Posted by: admin April 23, 2020 Leave a comment

Questions:

I came across a Room tutorial that makes use of the @PrimaryKey annotation on the class definition:

@Entity(foreignKeys = @ForeignKey(entity = User.class,
                              parentColumns = "id",
                              childColumns = "userId",
                              onDelete = CASCADE))
public class Repo {
    ...
}

Now, I have the following data class that want to use a primary key on:

@Parcel(Parcel.Serialization.BEAN) 
data class Foo @ParcelConstructor constructor(var stringOne: String,
                                              var stringTwo: String,
                                              var stringThree: String): BaseFoo() {

    ...
}

So, I just added the @Entity(tableName = "Foo", foreignKeys = @ForeignKey(entity = Bar::class, parentColumns = "someCol", childColumns = "someOtherCol", onDelete = CASCADE)) snippet on the top as well, but I can’t compile:

An annotation can’t be used as the annotations argument.

I wonder: how come (what I think is) the same concept working in Java but not in Kotlin? Also, is there a way to go around this?

All input is welcome.

How to&Answers:

This is the way to provide the annotation you’re looking for, with explicit arrays for the arguments, and no @ for the nested annotation’s creation:

@Entity(tableName = "Foo", 
    foreignKeys = arrayOf(
            ForeignKey(entity = Bar::class, 
                    parentColumns = arrayOf("someCol"), 
                    childColumns = arrayOf("someOtherCol"), 
                    onDelete = CASCADE)))

Since Kotlin 1.2, you can also use array literals:

@Entity(tableName = "Foo",
    foreignKeys = [
        ForeignKey(entity = Bar::class,
                parentColumns = ["someCol"],
                childColumns = ["someOtherCol"],
                onDelete = CASCADE)])