Reputation: 125
I'm using Spring Data and want to get data sorted by id from database.When I write findAllByOrderById,error occured.I think error code is "No property id found for type Employee!" But I don't know where to fix. Repository
@Repository
public interface EmployeeRepository extends JpaRepository<Employee, Long> {
public List<Employee> findAllByOrderById();
}
Model
@Table(name = "employees")
@Entity
@Getter
@Setter
public class Employee {
@Id
@Column(name = "emp_id", length = 8)
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer emp_id;
@Column(name = "emp_code", length = 20, nullable = false)
private String emp_code;
@Column(name = "emp_name", length = 20, nullable = false)
private String emp_name;
}
Controller
@Controller
public class DemoController {
@Autowired
EmployeeRepository empRepository;
@RequestMapping(value = "/", method = RequestMethod.GET)
public String index(Model model) {
List<Employee> emplist = empRepository.getAllEmployeesOrderById();
model.addAttribute("emplist", emplist);
return "view/index";
}
}
View
<body>
<div id="wrapper">
<div id="header">
<h1>日報管理システム</h1>
</div>
<div id="parent" th:include="app"></div>
</div>
<h1 th:text="${title}"></h1>
<table>
<tr th:each="emp : ${emplist}" th:object="${emp}">
<td th:text="*{emp_id}"></td>
<td th:text="*{emp_name}"></td>
</tr>
</table>
<br>
</body>
Error code
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'employeeRepository': FactoryBean threw exception on object creation; nested exception is java.lang.IllegalArgumentException: Failed to create query for method public abstract java.util.List spring.repositries.EmployeeRepository.findAllByOrderById()! No property id found for type Employee!
Caused by: java.lang.IllegalArgumentException: Failed to create query for method public abstract java.util.List spring.repositries.EmployeeRepository.findAllByOrderById()! No property id found for type Employee!
Caused by: org.springframework.data.mapping.PropertyReferenceException: No property id found for type Employee!
Upvotes: 3
Views: 3211
Reputation: 1450
Probably the naming you should use for the method should be:
findAllByOrderByEmp_IdAsc()
or
findAllByOrderByEmp_IdDesc()
depending on your need.
Full docs for query creation method naming here
As a recommendation also in Java, field naming usually used is camelCase.
So for example if your field would be named empId
your method will be named findAllByOrderByEmpIdAsc()
Upvotes: 2