[英]How to Sort element with object null in Spring Data Jpa
I have two entity below:我在下面有两个实体:
@Entity
public class Task {
...
@ManyToOne
private User user;
...
}
@Entity
public class User {
private String firstName;
private String lastName;
@OneToMany(mappedBy = "task")
private Set<Task> task;
...
}
How can I using Sort to Sort Task by firstName of User with User is null on top?我如何使用排序来按用户的名字排序任务,用户是 null?
I have try Sort.by(Sort.Order.asc("user.firstName").nullsFirst());
我试过
Sort.by(Sort.Order.asc("user.firstName").nullsFirst());
But I got the error: NullpointerException但我得到了错误:NullpointerException
You can create something like this.你可以创建这样的东西。
Criteria criteria = ...;
criteria.addOrder( Order.asc( "firstName" ).nulls(NullPrecedence.FIRST) );
for more information you can check this documentation.有关更多信息,您可以查看此文档。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.