如何在Spring中使用OrderBy与findAll。

huangapple go评论79阅读模式
英文:

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&lt;Employee, Long&gt; {
	public List&lt;Employee&gt; findAllByOrderById();

}

Model

@Table(name = &quot;employees&quot;)
@Entity
@Getter
@Setter
public class Employee {
	@Id
	@Column(name = &quot;emp_id&quot;, length = 8)
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Integer emp_id;

	@Column(name = &quot;emp_code&quot;, length = 20, nullable = false)
	private String emp_code;

	@Column(name = &quot;emp_name&quot;, length = 20, nullable = false)
	private String emp_name;
}

Controller

@Controller
public class DemoController {
	@Autowired
	EmployeeRepository empRepository;

	@RequestMapping(value = &quot;/&quot;, method = RequestMethod.GET)
	public String index(Model model) {
		List&lt;Employee&gt; emplist = empRepository.getAllEmployeesOrderById();

		model.addAttribute(&quot;emplist&quot;, emplist);
		return &quot;view/index&quot;;
	}
}

View

&lt;body&gt;
	&lt;div id=&quot;wrapper&quot;&gt;
		&lt;div id=&quot;header&quot;&gt;
			&lt;h1&gt;日報管理システム&lt;/h1&gt;
		&lt;/div&gt;
		&lt;div id=&quot;parent&quot; th:include=&quot;app&quot;&gt;&lt;/div&gt;
	&lt;/div&gt;
	&lt;h1 th:text=&quot;${title}&quot;&gt;&lt;/h1&gt;
	&lt;table&gt;
		&lt;tr th:each=&quot;emp : ${emplist}&quot; th:object=&quot;${emp}&quot;&gt;
			&lt;td th:text=&quot;*{emp_id}&quot;&gt;&lt;/td&gt;
			&lt;td th:text=&quot;*{emp_name}&quot;&gt;&lt;/td&gt;
		&lt;/tr&gt;
	&lt;/table&gt;

	&lt;br&gt;
&lt;/body&gt;

Error code

org.springframework.beans.factory.BeanCreationException: Error creating bean with name &#39;employeeRepository&#39;: 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()

huangapple
  • 本文由 发表于 2020年10月23日 21:20:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/64500826.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定