英文:
How to use OrderBy with findAll in Spring
问题
@Repository
public interface EmployeeRepository extends JpaRepository<Employee, Long> {
public List<Employee> findAllByOrderById();
}
@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
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";
}
}
<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>
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!
英文:
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!
答案1
得分: 1
可能您应该使用以下命名来命名该方法:
findAllByOrderByEmp_IdAsc()
或者
findAllByOrderByEmp_IdDesc()
根据您的需求。
有关查询创建方法命名的完整文档,请参见这里。
作为建议,在Java中,字段命名通常使用驼峰式命名法。
因此,例如,如果您的字段名为empId
,您的方法将被命名为findAllByOrderByEmpIdAsc()
。
英文:
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()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论