存储库未扩展 JpaRepository

Ary*_*rya 3 java spring spring-data-jpa spring-boot

我是使用 JPA 的新手,我正在在线阅读教程,所有这些教程都从 JPARespository 扩展而来,如下所示

从这个页面

https://www.callicoder.com/spring-boot-jpa-hibernate-postgresql-restful-crud-api-example/

package com.example.postgresdemo.repository;

import com.example.postgresdemo.model.Answer;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import java.util.List;

@Repository
public interface AnswerRepository extends JpaRepository<Answer, Long> {
    List<Answer> findByQuestionId(Long questionId);
}
Run Code Online (Sandbox Code Playgroud)

但在我的项目中 Eclipse 抱怨以下内容

The type JpaRepository<Property,Long> cannot be the superclass of PropertyRepository; a superclass must be a class
Run Code Online (Sandbox Code Playgroud)

下面是我的班级

package realestate.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import realestate.model.Property;

import java.util.List;

@Repository
public class PropertyRepository extends JpaRepository<Property, Long> {


}
Run Code Online (Sandbox Code Playgroud)

Man*_*H M 10

基本上,JPA 存储库是接口。

在您的代码中,您声明了一个类并使用接口扩展它。类可以实现接口,但不能扩展它。

因此请将类声明更改为接口,如下所示。

@Repository
public class PropertyRepository extends JpaRepository<Property, Long> {    

}
Run Code Online (Sandbox Code Playgroud)

@Repository
public interface PropertyRepository extends JpaRepository<Property, Long> {
    
}
Run Code Online (Sandbox Code Playgroud)