Java 无法在kotlin中调用静态panache方法

Java 无法在kotlin中调用静态panache方法,java,kotlin,quarkus,Java,Kotlin,Quarkus,第一个帖子。绝对是初学者。善待 我在和quarkus和kotlin玩arround 我有一个kotlin实体类: @Entity data class Fruit ( var name: String = "", var description: String = "" ) : PanacheEntity() 我有一个基于Java教程的资源类: @Path("/fruits") @ApplicationScoped public class FruitJReso

第一个帖子。绝对是初学者。善待

我在和quarkus和kotlin玩arround

我有一个kotlin实体类:

@Entity
data class Fruit (
        var name: String = "",
        var description: String = ""
) : PanacheEntity()
我有一个基于Java教程的资源类:

@Path("/fruits")
@ApplicationScoped
public class FruitJResource {
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public List<Fruit> getAll() {
        return Fruit.listAll();
    }
}
@Path(“/fruits”)
@适用范围
公共类水果资源{
@得到
@产生(MediaType.APPLICATION_JSON)
公共列表getAll(){
return Fruit.listAll();
}
}
这里一切都很好,水果继承自PanacheEntityBase,我可以访问listAll()

但是,, Kotlin中的同一类不:

@Path("/fruits")
@ApplicationScoped
class FruitResource {
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    fun getAll(): List<Fruit> = Fruit.listAll()
}
@Path(“/fruits”)
@适用范围
类资源{
@得到
@产生(MediaType.APPLICATION_JSON)
fun getAll():List=Fruit.listAll()
}
现在我了解了allready,这可能是因为kotlin不能从超类继承静态方法。 我读到,我应该直接从超类调用静态方法,但这在这里不起作用


所以我需要一个可能的解决办法的建议

目前
kotlin
scala
语言(1.4.1)的唯一解决方案是使用存储库模式: 见文件:

这是由于引用的问题github.com/quarkusio/quarkus/issues/4394造成的

因此,如果使用Kotlin,您只需定义一个新的
存储库

@ApplicationScoped
class FruitRepository: PanacheRepository<Fruit> {

    fun all(): List<Fruit> = findAll(Sort.by("name")).list<Fruit>()
}
@ApplicationScoped
类存储库:PanacheRepository{
fun all():List=findAll(Sort.by(“name”)).List()
}

Quarkus发布了一个扩展,它将Kotlin支持带到了panache(我认为它仍在预览中)

在Gradle中(如果您在项目中使用Gradle),您需要添加dependencie
实现“io.quarkus:quarkus hibernate orm panache kotlin”

要定义“静态”方法(Kotlin使用伴生对象来处理静态方法),您需要定义如下的伴生对象:

@Entity
open class Category : PanacheEntityBase {

@Id
@GeneratedValue
lateinit var id: UUID

// ...

companion object : PanacheCompanion<Category, UUID> {
    fun findByName(name: String) = find("name", name).firstResult()
    fun findActive() = list("active", true)
    fun deleteInactive() = delete("active", false)
}
@实体
开放类类别:PanacheEntityBase{
@身份证
@生成值
lateinit变量id:UUID
// ...
伴星对象:PanacheCompanion{
fun findByName(name:String)=find(“name”,name).firstResult()
fun findActive()=列表(“活动”,真)
fun deleteInactive()=删除(“活动”,false)
}
}

有关更多信息,您可以查看官方文档:

如果使用单元测试,请注意:至少对我来说,panache模拟扩展不适用于kotlin版本的panache。

您好!你可能读过吗?