Home > database >  Sprintboot Runtime error : Failed to create query for method public abstract com.example.demo.entity
Sprintboot Runtime error : Failed to create query for method public abstract com.example.demo.entity

Time:11-28

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:

@Repository
public interface DepartmentRepository extends JpaRepository<Department, Long> {

Department findByDepartmentId(long departmentid);
}

Department service: @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:

@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)! No property departmentId found for type Department! Did you mean 'departmentid'?

CodePudding user response:

Change your entity 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.

CodePudding user response:

You need to be cautious how you name the repository methods. In your repository code, you say Department findByDepartmentId(long departmentid);. So Spring is trying to make a search on the variable departmentId(Notice how camel casing matters here). Unfortunately, your entity class for the repository DepartmentRepository which is Department does not have any field by name departmentId and rather it has departmentid.

Here are 2 solutions:

  1. Rename repository method to Department findByDepartmentid(long departmentid);: THIS IS NOT RECOMMENDED.
  2. Change the departmentid in Department entity class to departmentId: RECOMMENDED AND BEST PRACTICE

You can find more details on naming repository methods here

  • Related