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

时间:2017-07-24 14:44:54

标签: android sqlite kotlin android-room android-architecture-components

我使用枚举作为带有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$1$2.invoke(CompileServiceImpl.kt:365)
    at org.jetbrains.kotlin.daemon.CompileServiceImpl$compile$1$2.invoke(CompileServiceImpl.kt:99)
    at org.jetbrains.kotlin.daemon.CompileServiceImpl$doCompile$2$$special$$inlined$withValidClientOrSessionProxy$lambda$1.invoke(CompileServiceImpl.kt:798)
    at org.jetbrains.kotlin.daemon.CompileServiceImpl$doCompile$2$$special$$inlined$withValidClientOrSessionProxy$lambda$1.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$2.invoke(CompileServiceImpl.kt:797)
    at org.jetbrains.kotlin.daemon.CompileServiceImpl$doCompile$2.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$1.invoke(CompileServiceImpl.kt:364)
    at org.jetbrains.kotlin.daemon.CompileServiceImpl$compile$1.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$1.run(Transport.java:200)
    at sun.rmi.transport.Transport$1.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$0(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$2.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$1.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

1 个答案:

答案 0 :(得分:0)

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

@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
)