[英]Sprintboot Runtime error : Failed to create query for method public abstract com.example.demo.entity.Department
Departmentclass:部门类:
@Entity
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Department {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long departmentid;
private String departmentName;
private String departmentAddress;
private String departmentcode;
}
Department Repository class:部门存储库 class:
@Repository
public interface DepartmentRepository extends JpaRepository<Department, Long> {
Department findByDepartmentId(long departmentid);
}
Department service: @Service @Slf4j public class DepartmentService {部门服务:@Service @Slf4j public class DepartmentService {
@Autowired
private DepartmentRepository departmentrepository;
public Department saveDepartment(Department department) {
log.info("Inside savedepartment method of deparment service");
return departmentrepository.save(department);
}
public Department finddepaartmentbyid(long departmentid) {
log.info("inside finddepartmentbyid mehtod of departmentservice");
return departmentrepository.findByDepartmentId(departmentid);
}
}
Department controller:部门controller:
@RestController
@RequestMapping("/departments")
@Slf4j
public class DepartmentController {
@Autowired
private DepartmentService departmentservice;
@PostMapping("/")
public Department saveDepartment(@RequestBody Department department)
{
log.info("Inside savedepatment method of department controller");
return departmentservice.saveDepartment(department);
}
@GetMapping("{id}")
public Department finddepartmentbyid(@PathVariable long departmentid)
{
log.info("inside finddepartmentbyid mehtod of department controller");
return departmentservice.finddepaartmentbyid(departmentid);
}
}
I am getting runtime error as Failed to create query for method public abstract com.example.demo.entity.Department com.example.demo.repository.DepartmentRepository.findByDepartmentId(long)?我收到运行时错误,因为无法为方法 public abstract com.example.demo.entity.Department com.example.demo.repository.DepartmentRepository.findByDepartmentId(long) 创建查询? No property departmentId found for type Department!没有找到类型部门的属性部门 ID! Did you mean 'departmentid'?您指的是 “departmentid” 吗?
Change your entity class:更改您的实体 class:
@Entity
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Department {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long departmentId; // not departmentid !
private String departmentName;
private String departmentAddress;
private String departmentcode;
}
The error is telling you there's no departmentId .错误告诉你没有departmentId 。
You need to be cautious how you name the repository methods.您需要谨慎命名存储库方法。 In your repository code, you say Department findByDepartmentId(long departmentid);
在您的存储库代码中,您说Department findByDepartmentId(long departmentid);
. . So Spring is trying to make a search on the variable departmentId
(Notice how camel casing matters here).所以 Spring 正在尝试对变量departmentId
进行搜索(请注意这里的驼峰式大小写如何重要)。 Unfortunately, your entity class for the repository DepartmentRepository
which is Department
does not have any field by name departmentId
and rather it has departmentid
.不幸的是,存储库DepartmentRepository
的实体 class 是Department
没有任何名称为departmentId
的字段,而是具有departmentid
。
Here are 2 solutions:这里有2个解决方案:
Department findByDepartmentid(long departmentid);
将存储库方法重命名为Department findByDepartmentid(long departmentid);
: THIS IS NOT RECOMMENDED . :这不是推荐的。departmentid
in Department
entity class to departmentId
: RECOMMENDED AND BEST PRACTICE将Department
实体 class 中的departmentId
标识更改为departmentid
标识:推荐和最佳实践You can find more details on naming repository methods here您可以在此处找到有关命名存储库方法的更多详细信息
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.