49

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.

Chisko
  • 3,092
  • 6
  • 27
  • 45

1 Answers1

121

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)])
zsmb13
  • 85,752
  • 11
  • 221
  • 226
  • 1
    someCol is part of class for your example Foo and someOtherCol is for xlass BAr ? – kiki Kala2 May 13 '20 at 09:20
  • Actually, the other way around. The child columns are the ones from the current entity (so `Foo` in this case), and the parent columns are from the other entity (`Bar` in this case). – zsmb13 May 13 '20 at 16:47