我从 Quarkus 教程开始:https ://quarkus.io/guides/mongodb-panache用于学习如何在 Quarkus 中使用 Mongo,但是当我尝试findByIdOptional
从PanacheMongoRepository
控制台使用时我的实体正存储在数据库中显示此错误:
Caused by: java.lang.IllegalStateException: This method is normally automatically overridden in subclasses
at io.quarkus.mongodb.panache.runtime.MongoOperations.implementationInjectionMissing(MongoOperations.java:633)
at io.quarkus.mongodb.panache.PanacheMongoRepositoryBase.findByIdOptional(PanacheMongoRepositoryBase.java:102)
at com.basketmaster.backend.common.infraestructure.CrudMongoRepository.findById(CrudMongoRepository.kt:25)
interface CrudRepository<M : Model<*>> {
fun save(model: M): M
fun findById(id: String): M
}
@ApplicationScoped
class CrudMongoRepository<M : Model<*>> : CrudRepository<M>, PanacheMongoRepository<M> {
override fun save(model: M): M {
persistOrUpdate(model)
return model
}
override fun findById(id: String): M {
val optional = super.findByIdOptional(ObjectId(id))
return optional.orElseThrow { NotFoundException() }
}
}
我注意到接口 PanacheMongoRepository 从 PanacheMongoRepositoryBase 继承,但 findByIdOptional 没有实现:
public interface PanacheMongoRepositoryBase<Entity, Id> {
// ...
@GenerateBridge(targetReturnTypeErased = true)
default Entity findById(Id id) {
throw INSTANCE.implementationInjectionMissing();
}
@GenerateBridge
default Optional<Entity> findByIdOptional(Id id) {
throw INSTANCE.implementationInjectionMissing();
}
}
我应该如何使用 Panache 通过 id 查找实体?我不明白为什么这些方法没有实现,但是本教程使用它们并且它可以正常工作。