5

I'm new to Kotlin and I wish to convert my java model classes with data classes, is it possible? I mean does Ormlite support this?

hotkey
  • 140,743
  • 39
  • 371
  • 326
Zeezl
  • 123
  • 4
  • 15
  • 1
    I tried it with JPA, and it doesn't work with Kotlin quite well, here's my question about it: http://stackoverflow.com/questions/32038177/kotlin-with-jpa-default-constructor-hell. Don't know about Ormlite though, and would be glad if it does. – hotkey Feb 19 '16 at 15:45
  • Can you explain how Ormlite uses classes? does it construct them, if so does it need an empty default constructor? Did you try an experiment? – Jayson Minard Feb 19 '16 at 17:05

3 Answers3

7

I'm using OrmLite with Kotlin's data classes with no problem. The key is to specify default values for all fields, then Kotlin generates an empty constructor for the data class:

@DatabaseTable(tableName = "sample_table")
data class SampleRecord(
        @DatabaseField(id = true)
        var id: String? = null,

        @DatabaseField(canBeNull = false)
        var numField: Int? = null,

        @DatabaseField
        var strField: String = "",

        @DatabaseField
        var timestamp: Date = Date()
)

» Working example on GitHub

juzraai
  • 5,693
  • 8
  • 33
  • 47
  • This doesn't seem to work if the properties are vals – starkej2 Jul 08 '19 at 18:58
  • @starkej2 Of course, since OrmLite works with noarg constructor + setters, and `val`s don't have setters. – juzraai Jul 12 '19 at 06:24
  • I thought OrmLite used reflection to initialize the properties? @juzraai https://stackoverflow.com/a/16780928/1169961 – starkej2 Jul 12 '19 at 18:41
  • Well, then I was wrong in my comment above. :) So it is working with `final` fields in Java, but not with `val`s in Kotlin? – juzraai Jul 12 '19 at 18:47
3

I converted my daos to normal classes without problems

import com.j256.ormlite.field.DatabaseField
import com.j256.ormlite.table.DatabaseTable

@DatabaseTable(tableName = HabitDao.TABLE)
class HabitDao() {

    companion object {
        const val TABLE = "habitdao"
        const val ORDER = "order"
        const val ID = "id"
    }

    @DatabaseField(columnName = ID, generatedId = true)
    var id: Int = 0

    @DatabaseField(index = true)
    lateinit var title: String

    @DatabaseField
    lateinit var intention: String

    @DatabaseField(columnName = ORDER)
    var order: Int = 0

    constructor(title: String, intention: String) : this() {
        this.title = title
        this.intention = intention
    }

    override fun toString(): String {
        return title
    }
}

You just need to provide empty constructor (see the one in the class definition). Also lateinit makes properties easier to use later on.

Edit: Data classes seem to add features that are useful when you need to serialize those objects. Ormlite is able to handle normal a.k.a. Java classes already so there is no need to do that. Moreover data class is expected to contain all its fields in the constructor and you don't want the id field to be present there.

damienix
  • 6,463
  • 1
  • 23
  • 30
0

No, ORMLite cannot be used with Kotlin data classes (as of version 1.1.2) because of ORMLite requirement to annotate the fields with @DatabaseField, which is not possible with fields declared with the data class syntax.

m0skit0
  • 25,268
  • 11
  • 79
  • 127