简体   繁体   English

Sprintboot 运行时错误:无法为方法公共抽象 com.example.demo.entity.Department 创建查询

[英]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个解决方案:

  1. Rename repository method to Department findByDepartmentid(long departmentid);将存储库方法重命名为Department findByDepartmentid(long departmentid); : THIS IS NOT RECOMMENDED . :这不是推荐的。
  2. Change the departmentid in Department entity class to departmentId : RECOMMENDED AND BEST PRACTICEDepartment实体 class 中的departmentId标识更改为departmentid标识:推荐和最佳实践

You can find more details on naming repository methods here您可以在此处找到有关命名存储库方法的更多详细信息

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 验证方法公共摘要查询失败 - Validation failed for query for method public abstract 提交为方法公共摘要创建查询 - Filed to create query for method public abstract 无法为公共抽象 org.springframework.data.domain.Page com.example.repository.DocumentsRepository.findBytypeid 创建查询 - Could not create query for public abstract org.springframework.data.domain.Page com.example.repository.DocumentsRepository.findBytypeid Spring JPA:方法公共抽象方法查询验证失败 - Spring JPA : Validation failed for query for method public abstract method Caused by: java.lang.IllegalArgumentException: Validation failed for query for method public abstract java.lang.Integer com.dyplom.repository - Caused by: java.lang.IllegalArgumentException: Validation failed for query for method public abstract java.lang.Integer com.dyplom.repository 查询方法 public abstract java.util.List 的验证失败 - Validation failed for query for method public abstract java.util.List Hibernate 错误:查询方法公共抽象 java.util.List 的验证失败 - Hibernate error: Validation failed for query for method public abstract java.util.List init 方法调用失败; 嵌套异常是 java.lang.IllegalArgumentException:查询方法公共抽象的验证失败 - Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Validation failed for query for method public abstract 无法将java.lang.String类型的属性值转换为属性部门所需的com.akybenko.entity.Department类型; - Failed to convert property value of type java.lang.String to required type com.akybenko.entity.Department for property department; JSP - 实例化 servlet class [com.example.demo.HelloServlet] 时出错 - JSP - Error instantiating servlet class [com.example.demo.HelloServlet]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM