2

I have the following Kotlin class that I'm using as a Room entity

@Entity(tableName = "subscriptions")
data class Subscription(@ColumnInfo(name = "title")
                        var name: String,
                        @ColumnInfo(name = "description")
                        var description: String,
                        @ColumnInfo(name = "price")
                        var price: Float,
                        @ColumnInfo(name = "payment_method")
                        var paymentMethod: String,
                        @ColumnInfo(name = "frequency")
                        var frequencyNum: Int,
                        @ColumnInfo(name = "interval")
                        @TypeConverters(FrequencyTypeConverter::class)
                        var frequencyType: FrequencyType,
                        @ColumnInfo(name = "start_date")
                        @TypeConverters(DateTypeConverter::class)
                        var startDate: Date,
                        @PrimaryKey(autoGenerate = true)
                        val id: Int = 0) : Comparable<Subscription>

however, when I build I get the following error:

private com.owenlejeune.subscriptions.model.FrequencyType frequencyType;
                                                              ^/Volumes/Data/Code/HonorsProject/ModernSubscriptions/app/build/tmp/kapt3/stubs/debug/com/owenlejeune/subscriptions/model/Subscription.java:29: error: Cannot figure out how to save this field into database. You can consider adding a type converter for it.

My FrequencyType converter class looks like

class FrequencyTypeConverter {

    @TypeConverter
    fun fromFrequencyType(value: FrequencyType): Int = value.ordinal

    @TypeConverter
    fun toFrequencyType(value: Int): FrequencyType = FrequencyType.values()[value]

}

Any ideas why I'm still receiving this error?

owenlejeune
  • 135
  • 1
  • 7

2 Answers2

2

I'm going to have to disagree with the answer from @DivijGupta, you can put the @TypeConverters annotation on entity fields and it will limit the scope of the converter to that particular field as I am sure you intended. The documentation here actually lists the usage as:

If you put it on an Entity field, only that field will be able to use it.

The error is actually to do with how Kotlin translates the annotations into Java bytecode. If you have a look at what adding an annotation to a field actually does in the decompiled code, you can see that Kotlin places it on the constructor parameter, not the field itself. Therefore as Room doesn't see an annotation on the field it gives you the error.

To fix this you have to use an extra field qualifier when annotating, in this instance:

@field:TypeConverters(FrequencyTypeConverter::class)

and you can read more about this in the Kotlin documentation on Annotation use-site targets.


Just as a side note, the answer from @DivijGupta would fix the error as the problem that I have outlined wouldn't exist, but it is just a workaround.

Henry Twist
  • 5,666
  • 3
  • 19
  • 44
-2

You need to add @TypeConverters annotation to your database class, not in the entity class.

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
Divij Gupta
  • 165
  • 9