Spring 数据 jpa:QuerySyntaxException: 部门未映射

Spring data jpa:QuerySyntaxException: department is not mapped

我正在使用 spring 数据 jpa.I 使用 @query 批注编写查询并尝试获取 table 中的所有列但是在执行时我收到部门 table 未映射。

部门资料库

 @Repository
    public interface DepartmentsRepository extends JpaRepository<Department, Long> 
    {
        @Query(value = "select d from department d")
        public List<Department> findColumns();
    }

部门class

@Entity
@Table(name="department")
public class Department 
{
@Id
@Column(name="ndept_id")
public int ndept_id;

@Column(name="sdept_name")
public String sdept_name;

@Column(name="ninst_id")
public int ninst_id;

@Column(name="bis_locked")
public boolean bis_locked;

@Column(name="sclient_dept_id")
public String sclient_dept_id;

@Column(name="nsurvey_method_id")
public  int nsurvey_method_id;

@Column(name="bis_jointuse")
public boolean bis_jointuse;

@Column(name="ntemp_dept_id")
public int ntemp_dept_id;

@Column(name="balternate_jointuse_percentage")
public boolean balternate_jointuse_percentage;

@Column(name="ndiv_id")
public Integer ndiv_id;

针对实体的JPQL查询,不会直接影响数据库。所以使用模型名称而不是 table 名称

@Repository
    public interface DepartmentsRepository extends JpaRepository<Department, Integer> 
    {
        @Query("select d from Department d")
        public List<Department> findColumns();
    }

JPA 不处理 table 名称,因此如果您想用 JPA 查询语言 (JPQL) 编写查询,请使用完全分类的 (package_name.class_name) 实体名称而不是 table 名字。

因此您的查询将如下所示

@Query(value = "select d from package_name.Department d")
public List<Department> findColumns();

或者你也可以 运行 普通 sql 在 @Query 注释中使用 nativeQuery=true 查询,所以你的代码看起来像

@Query(value = "select d from department d", nativeQuery=true)
public List<Department> findColumns();