在Kotlin中使用Room的@ForeignKey作为@Entity参数
我遇到了一个Room 教程 ,它使用了类定义上的@PrimaryKey
注释:
@Entity(foreignKeys = @ForeignKey(entity = User.class, parentColumns = "id", childColumns = "userId", onDelete = CASCADE)) public class Repo { ... }
现在,我有以下想要使用主键的数据类:
@Parcel(Parcel.Serialization.BEAN) data class Foo @ParcelConstructor constructor(var stringOne: String, var stringTwo: String, var stringThree: String): BaseFoo() { ... }
所以,我刚刚在@Entity(tableName = "Foo", foreignKeys = @ForeignKey(entity = Bar::class, parentColumns = "someCol", childColumns = "someOtherCol", onDelete = CASCADE))
添加了@Entity(tableName = "Foo", foreignKeys = @ForeignKey(entity = Bar::class, parentColumns = "someCol", childColumns = "someOtherCol", onDelete = CASCADE))
片段,但我无法编译:
注释不能用作注释参数。
我想知道:为什么(我认为是)在Java中使用相同的概念但在Kotlin中却没有? 还有,有办法解决这个问题吗?
欢迎所有输入。
这是提供您正在寻找的注释的方法,使用显式的参数数组,并且没有@
用于嵌套注释的创建:
@Entity(tableName = "Foo", foreignKeys = arrayOf( ForeignKey(entity = Bar::class, parentColumns = arrayOf("someCol"), childColumns = arrayOf("someOtherCol"), onDelete = CASCADE)))
从Kotlin 1.2开始 ,你也可以使用数组文字:
@Entity(tableName = "Foo", foreignKeys = [ ForeignKey(entity = Bar::class, parentColumns = ["someCol"], childColumns = ["someOtherCol"], onDelete = CASCADE)])