No property 'id' found for type 'Department', eventhough i given @id annotation, and tried with mySql database and h2 database

Viewed 24
@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Department {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long departmentId;

private String departmentName;
private String departmentAddress;
private String departmentCode;


}

the above code is my entity class Department

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

Department findDepartmentById(Long departmentId);
// Department getDepartmentById(Long deptId);
}

my repositoy interface DepartmentRepository

I am getting the below error. Few days back I did the same code , everything same , it worked but now it is throwing this error, Please help me in solving this error

com.wipro.DepartmentmicroService.repository.DepartmentRepository.findDepartmentById(java.lang.String)! No property 'id' found for type 'Department'

2 Answers
@Repository
public interface DepartmentRepository extends JpaRepository<Department,Long> {
    Department findByDepartmentId(Long departmentId);
}

In repository the method name should be findByDepartmentId, because , in sql it implements like select * from Department where departmentId = "some number"

Or you can use Optional<Department> findById(Long id) from JpaRepository.

It accepts @Id property.

Related