gpt4 book ai didi

java - 无法在 kotlin 中调用静态 panache 方法

转载 作者:行者123 更新时间:2023-12-02 12:52:39 26 4
gpt4 key购买 nike

第一篇文章。绝对的新人。善良

我正在玩 quarkus 和 kotlin。

我有这个 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();
}
}

一切都很好,Fruit 继承自 PanacheEntityBase,我可以访问 listAll()

但是,Kotlin 中的 Same Class 不会:

@Path("/fruits")
@ApplicationScoped
class FruitResource {
@GET
@Produces(MediaType.APPLICATION_JSON)
fun getAll(): List<Fruit> = Fruit.listAll()
}

现在我已经了解到,这可能是由于 kotlin 无法从父类(super class)继承静态方法。我读到,我应该直接从父类(super class)调用静态方法,但这在这里行不通。

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

最佳答案

目前 (1.4.1) kotlinscala 语言的唯一解决方案是使用 Repository 模式:请参阅文档:https://quarkus.io/guides/hibernate-orm-panache#solution-2-using-the-repository-pattern

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

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

@ApplicationScoped
class FruitRepository: PanacheRepository<Fruit> {

fun all(): List<Fruit> = findAll(Sort.by("name")).list<Fruit>()
}

关于java - 无法在 kotlin 中调用静态 panache 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59161593/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com