基于Kotlin的PanacheEntity抛出运行时异常

时间:2019-10-05 15:30:01

标签: quarkus

使用kotlin正确配置PanacheEntity类时遇到问题。这是我的实体:

package rs.dodalovic.ordinacija.patients.db

import io.quarkus.hibernate.orm.panache.PanacheEntity
import javax.persistence.*

@Entity
@Table(name = "pacijent", schema = "ordinacija")
class Patient(
    @field:Column(name = "BR") @field:Id @field:GeneratedValue(strategy = GenerationType.AUTO) var id: Int,
    @field:Column(name = "Ime") var name: String,
    @field:Column(name = "Prezime") var lastname: String,
    @field:Column(name = "ImeOca") var fatherName: String,
    @field:Column(name = "JMBG") var personalNumber: String,
    @field:Column(name = "DatumRodjenja") var dob: String,
    @field:Column(name = "Adresa") var address: String,
    @field:Column(name = "Telefon") var phone: String,
    @field:Column(name = "Notes") var notes: String
) : PanacheEntity() {
    companion object {
        fun findById(id: Int): Patient? {
            return find<Patient>("id", id).firstResult()
        }

        fun deleteById(id: String) {
            PanacheEntity.delete("id", id).also {
                println("Attempt to delete by id $id resulted in $it deleted rows")
            }
        }
    }
}

当我调用findById方法时,出现以下异常:

        at java.lang.Thread.run(Thread.java:748)
        at org.jboss.threads.JBossThread.run(JBossThread.java:479)
Caused by: java.lang.IllegalStateException: This method is normally automatically overridden in subclasses: did you forget to annotate your entity with @Entity?
        at io.quarkus.hibernate.orm.panache.runtime.JpaOperations.implementationInjectionMissing(JpaOperations.java:367)
        at io.quarkus.hibernate.orm.panache.PanacheEntityBase.find(PanacheEntityBase.java:115)

1 个答案:

答案 0 :(得分:0)

正如Github问题中提到的那样,问题本质上是Kotlin编译器在生成invokeStatic字节码指令时没有使用实体作为目标,而是使用PanacheEntity。

它看起来像:

invokestatic  #xy                 // Method io/quarkus/hibernate/orm/panache/PanacheEntity.find:(Ljava/lang/String;[Ljava/lang/Object;)Lio/quarkus/hibernate/orm/panache/PanacheQuery;

而Java中的类似示例具有这样的字节码:

invokestatic  #xy                  // Method io/quarkus/hibernate/orm/panache/Fruit.find:(Ljava/lang/String;[Ljava/lang/Object;)Lio/quarkus/hibernate/orm/panache/PanacheQuery;

我还应该提到,当尝试从实体的伴随对象内部调用Entity方法的静态方法时会发生这种情况。