将 Enum 作为 Room 数据库 Library 的主键时出错

Error with making Enum as primary key at Room database Library

我正在使用枚举作为带有 TypeConvertor 的 Room 数据库的主键,它不会在 运行 时编译并抛出错误

这是我的转换器

class QuranIndexConverter {

@TypeConverter
fun toQuranIndex(index: String): QuranIndex {
    return QuranIndex.valueOf(index)
}

@TypeConverter
fun toStringIndex(quranIndex: QuranIndex): String {
    return quranIndex.name
}}

这里是道

    @Dao
interface SuraDao {

    @Query("SELECT * FROM " + Constants.TABLE_NAME)
    fun getAll(): LiveData<List<Sura>>

    @Insert
    fun insertAll(suras: List<Sura>)

    @Insert
    fun insert(sura: Sura)

    @Delete
    fun deleteAll(suras: List<Sura>)

    @Delete
    fun delete(sura: Sura)}

这是模块

@Entity(tableName = Constants.TABLE_NAME)
 data class Sura(
    @PrimaryKey
    val quranIndex: QuranIndex = QuranIndex.ABS,

    @ColumnInfo(name = "sura_name")
    val suraName: String = "Abasa",

    @ColumnInfo(name = "download_url")
    val downloadUrl: String = "www.google.com")

这里是房间数据库

@Database(
    entities = arrayOf(Sura::class),
    version = 1)
@TypeConverters(QuranIndexConverter::class)
abstract class Appdatabase : RoomDatabase() {
abstract fun suraDao(): SuraDao
}

错误信息

    e: H:\Work\Android\Inmaa\Nour\NourPLayerArch\app\build\tmp\kapt3\stubs\debug\com\abdelmeged\ahmed\nourplayerarch\db\entity\Sura.java:31: error: Room cannot pick a constructor since multiple constructors are suitable. Try to annotate
    e: 

    e:     public Sura(@org.jetbrains.annotations.NotNull()
    e:            ^
    e:   unwanted constructors with @Ignore.
    e: H:\Work\Android\Inmaa\Nour\NourPLayerArch\app\build\tmp\kapt3\stubs\debug\com\abdelmeged\ahmed\nourplayerarch\db\entity\Sura.java:38: error: Room cannot pick a constructor since multiple constructors are suitable. Try to annotate
    e: 

    e:     public Sura() {
    e:            ^
    e:   unwanted constructors with @Ignore.
    e: H:\Work\Android\Inmaa\Nour\NourPLayerArch\app\build\tmp\kapt3\stubs\debug\com\abdelmeged\ahmed\nourplayerarch\db\entity\Sura.java:8: error: Cannot find setter for field.
   e: 

   e:     private final com.abdelmeged.ahmed.nourplayerarch.utils.QuranIndex quranIndex = null;
   e:                                                                        ^
   e: H:\Work\Android\Inmaa\Nour\NourPLayerArch\app\build\tmp\kapt3\stubs\debug\com\abdelmeged\ahmed\nourplayerarch\db\entity\Sura.java:11: error: Cannot find setter for field.
   e: 

   e:     private final java.lang.String suraName = null;
   e:                                    ^
   e: H:\Work\Android\Inmaa\Nour\NourPLayerArch\app\build\tmp\kapt3\stubs\debug\com\abdelmeged\ahmed\nourplayerarch\db\entity\Sura.java:14: error: Cannot find setter for field.
   e: 

   e:     private final java.lang.String downloadUrl = null;
   e:                                    ^

w: H:\Work\Android\Inmaa\Nour\NourPLayerArch\app\build\tmp\kapt3\stubs\debug\com\abdelmeged\ahmed\nourplayerarch\db\Appdatabase.java:6: warning: Schema export directory is not provided to the annotation processor so we cannot export the schema. You can either provide `room.schemaLocation` annotation processor argument OR set exportSchema to false.
w: 

w: public abstract class Appdatabase extends android.arch.persistence.room.RoomDatabase {
w:                 ^

e: java.lang.IllegalStateException: failed to analyze: org.jetbrains.kotlin.kapt3.diagnostic.KaptError: Error while annotation processing
    at org.jetbrains.kotlin.analyzer.AnalysisResult.throwIfError(AnalysisResult.kt:57)
    at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.compileModules(KotlinToJVMBytecodeCompiler.kt:144)
    at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.doExecute(K2JVMCompiler.kt:167)
    at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.doExecute(K2JVMCompiler.kt:55)
    at org.jetbrains.kotlin.cli.common.CLICompiler.exec(CLICompiler.java:182)
    at org.jetbrains.kotlin.daemon.CompileServiceImpl.execCompiler(CompileServiceImpl.kt:397)
    at org.jetbrains.kotlin.daemon.CompileServiceImpl.access$execCompiler(CompileServiceImpl.kt:99)
    at org.jetbrains.kotlin.daemon.CompileServiceImpl$compile.invoke(CompileServiceImpl.kt:365)
    at org.jetbrains.kotlin.daemon.CompileServiceImpl$compile.invoke(CompileServiceImpl.kt:99)
    at org.jetbrains.kotlin.daemon.CompileServiceImpl$doCompile$$special$$inlined$withValidClientOrSessionProxy$lambda.invoke(CompileServiceImpl.kt:798)
    at org.jetbrains.kotlin.daemon.CompileServiceImpl$doCompile$$special$$inlined$withValidClientOrSessionProxy$lambda.invoke(CompileServiceImpl.kt:99)
    at org.jetbrains.kotlin.daemon.common.DummyProfiler.withMeasure(PerfUtils.kt:137)
    at org.jetbrains.kotlin.daemon.CompileServiceImpl.checkedCompile(CompileServiceImpl.kt:825)
    at org.jetbrains.kotlin.daemon.CompileServiceImpl.access$checkedCompile(CompileServiceImpl.kt:99)
    at org.jetbrains.kotlin.daemon.CompileServiceImpl$doCompile.invoke(CompileServiceImpl.kt:797)
    at org.jetbrains.kotlin.daemon.CompileServiceImpl$doCompile.invoke(CompileServiceImpl.kt:99)
    at org.jetbrains.kotlin.daemon.CompileServiceImpl.ifAlive(CompileServiceImpl.kt:1004)
    at org.jetbrains.kotlin.daemon.CompileServiceImpl.ifAlive$default(CompileServiceImpl.kt:865)
    at org.jetbrains.kotlin.daemon.CompileServiceImpl.doCompile(CompileServiceImpl.kt:791)
    at org.jetbrains.kotlin.daemon.CompileServiceImpl.access$doCompile(CompileServiceImpl.kt:99)
    at org.jetbrains.kotlin.daemon.CompileServiceImpl$compile.invoke(CompileServiceImpl.kt:364)
    at org.jetbrains.kotlin.daemon.CompileServiceImpl$compile.invoke(CompileServiceImpl.kt:99)
    at org.jetbrains.kotlin.daemon.CompileServiceImpl.ifAlive(CompileServiceImpl.kt:1004)
    at org.jetbrains.kotlin.daemon.CompileServiceImpl.ifAlive$default(CompileServiceImpl.kt:865)
    at org.jetbrains.kotlin.daemon.CompileServiceImpl.compile(CompileServiceImpl.kt:336)
    at sun.reflect.GeneratedMethodAccessor80.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:346)
    at sun.rmi.transport.Transport.run(Transport.java:200)
    at sun.rmi.transport.Transport.run(Transport.java:197)
    at java.security.AccessController.doPrivileged(Native Method)
    at sun.rmi.transport.Transport.serviceCall(Transport.java:196)
    at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:568)
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:826)
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run[=14=](TCPTransport.java:683)
    at java.security.AccessController.doPrivileged(Native Method)
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:682)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
Caused by: org.jetbrains.kotlin.kapt3.diagnostic.KaptError: Error while annotation processing
    at org.jetbrains.kotlin.kapt3.AnnotationProcessingKt.doAnnotationProcessing(annotationProcessing.kt:90)
    at org.jetbrains.kotlin.kapt3.AnnotationProcessingKt.doAnnotationProcessing$default(annotationProcessing.kt:42)
    at org.jetbrains.kotlin.kapt3.AbstractKapt3Extension.runAnnotationProcessing(Kapt3Extension.kt:205)
    at org.jetbrains.kotlin.kapt3.AbstractKapt3Extension.analysisCompleted(Kapt3Extension.kt:166)
    at org.jetbrains.kotlin.kapt3.ClasspathBasedKapt3Extension.analysisCompleted(Kapt3Extension.kt:82)
    at org.jetbrains.kotlin.resolve.jvm.TopDownAnalyzerFacadeForJVM$analyzeFilesWithJavaIntegration.invoke(TopDownAnalyzerFacadeForJVM.kt:89)
    at org.jetbrains.kotlin.resolve.jvm.TopDownAnalyzerFacadeForJVM.analyzeFilesWithJavaIntegration(TopDownAnalyzerFacadeForJVM.kt:99)
    at org.jetbrains.kotlin.resolve.jvm.TopDownAnalyzerFacadeForJVM.analyzeFilesWithJavaIntegration$default(TopDownAnalyzerFacadeForJVM.kt:76)
    at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler$analyze.analyze(KotlinToJVMBytecodeCompiler.kt:365)
    at org.jetbrains.kotlin.cli.common.messages.AnalyzerWithCompilerReport.analyzeAndReport(AnalyzerWithCompilerReport.kt:105)
    at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.analyze(KotlinToJVMBytecodeCompiler.kt:354)
    at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.compileModules(KotlinToJVMBytecodeCompiler.kt:139)
    ... 39 more

我有这个错误,因为 kotlin 显然为具有默认参数值的单个 kotlin 构造函数生成多个 java 构造函数。工作代码我编辑数据 class 是这样的:

@Entity(tableName = Constants.TABLE_NAME)
data class Sura(
        @PrimaryKey
        val quranIndex: QuranIndex,

        @ColumnInfo(name = "sura_name")
        val suraName: String ,

        @ColumnInfo(name = "download_url")
        val downloadUrl: String
)