如何过滤 spring 数据 JPA 中的 @OneToMany 映射字段?
How to filter a @OneToMany mapped field in spring data JPA?
我正在尝试使用 Spring Boot REST api 构建费用跟踪器应用程序。以下是我创建的实体类。
用户模型
@Entity
public class User{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@NotBlank(message = "username is required !")
@Column(unique = true)
@Size(min = 10,max = 10,message = "username must be exactly 10 characters in length !")
private String username;
@Email(message = "Must be a valid Email !")
@NotBlank(message = "Email is required !")
@Size(min = 5, max = 256, message = "Email must be between 5 and 256 characters in length !")
private String email;
@NotBlank(message = "Password is required !")
@Size(min = 8 , message = "Password must be greater than 8 characters in length !")
private String password;
@NotBlank(message = "FirstName is required !")
@Size(min = 3 , max = 100 , message = "FirstName must be between 3 and 100 characters in length !")
private String firstName;
@NotBlank(message = "LastName is required !")
@Size(min = 3 , max = 100 , message = "LastName must be between 3 and 100 characters in length !")
private String lastName;
@OneToOne(cascade = CascadeType.ALL)
private Balance balance;
@OrderBy("createdAt DESC")
@OneToMany(fetch = FetchType.LAZY , cascade = CascadeType.ALL)
private List<Expense> expensesList;
@Temporal(TemporalType.TIMESTAMP)
private Date createdAt;
......
}
费用模型
@Entity
@Table(name = "expense_tracker_expenses")
public class Expense {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@ManyToOne(fetch = FetchType.LAZY)
private User user;
@NotBlank(message = "Title is required !")
@Size(min = 3 , max = 60 , message = "Title must be between 3 and 8 characters !")
private String title;
@Size(max = 256, message = "Note must be less than 256 characters !")
private String note;
@DecimalMin(value = "0.0",message = "Amount cannot be less than 0.0 !")
private double amount;
@NotBlank(message = "Category is required !")
@Pattern(regexp = "Clothing|Entertainment|Food|Fuel|Health|Salary|Misc", message = "Invalid category !")
private String category;
@Temporal(TemporalType.TIMESTAMP)
private Date createdAt;
...
}
我创建了一个方法
public List<Expense> findByCreatedAtBetween(Date startCreatedAt,Date endCreatedAt);
获取日期之间的费用列表,但returns日期之间所有用户的费用。
如何获取某个用户在开始日期和结束日期之间的费用清单?
有两种方法可以做到
- 使用 JPA 使用 AND 谓词进行查询。
public List<Expense> findByUserAndCreatedAtBetween(User user,
Date startCreatedAt,
Date endCreatedAt);
- 获取用户所有费用后在Java中过滤。
user.getExpensesList().stream().filter(item ->
item.getCreatedAt().after(startDate) &&
item.getCreatedAt().before(endDate)
).collect(Collectors.toList());
我正在尝试使用 Spring Boot REST api 构建费用跟踪器应用程序。以下是我创建的实体类。
用户模型
@Entity
public class User{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@NotBlank(message = "username is required !")
@Column(unique = true)
@Size(min = 10,max = 10,message = "username must be exactly 10 characters in length !")
private String username;
@Email(message = "Must be a valid Email !")
@NotBlank(message = "Email is required !")
@Size(min = 5, max = 256, message = "Email must be between 5 and 256 characters in length !")
private String email;
@NotBlank(message = "Password is required !")
@Size(min = 8 , message = "Password must be greater than 8 characters in length !")
private String password;
@NotBlank(message = "FirstName is required !")
@Size(min = 3 , max = 100 , message = "FirstName must be between 3 and 100 characters in length !")
private String firstName;
@NotBlank(message = "LastName is required !")
@Size(min = 3 , max = 100 , message = "LastName must be between 3 and 100 characters in length !")
private String lastName;
@OneToOne(cascade = CascadeType.ALL)
private Balance balance;
@OrderBy("createdAt DESC")
@OneToMany(fetch = FetchType.LAZY , cascade = CascadeType.ALL)
private List<Expense> expensesList;
@Temporal(TemporalType.TIMESTAMP)
private Date createdAt;
......
}
费用模型
@Entity
@Table(name = "expense_tracker_expenses")
public class Expense {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@ManyToOne(fetch = FetchType.LAZY)
private User user;
@NotBlank(message = "Title is required !")
@Size(min = 3 , max = 60 , message = "Title must be between 3 and 8 characters !")
private String title;
@Size(max = 256, message = "Note must be less than 256 characters !")
private String note;
@DecimalMin(value = "0.0",message = "Amount cannot be less than 0.0 !")
private double amount;
@NotBlank(message = "Category is required !")
@Pattern(regexp = "Clothing|Entertainment|Food|Fuel|Health|Salary|Misc", message = "Invalid category !")
private String category;
@Temporal(TemporalType.TIMESTAMP)
private Date createdAt;
...
}
我创建了一个方法
public List<Expense> findByCreatedAtBetween(Date startCreatedAt,Date endCreatedAt);
获取日期之间的费用列表,但returns日期之间所有用户的费用。
如何获取某个用户在开始日期和结束日期之间的费用清单?
有两种方法可以做到
- 使用 JPA 使用 AND 谓词进行查询。
public List<Expense> findByUserAndCreatedAtBetween(User user,
Date startCreatedAt,
Date endCreatedAt);
- 获取用户所有费用后在Java中过滤。
user.getExpensesList().stream().filter(item ->
item.getCreatedAt().after(startDate) &&
item.getCreatedAt().before(endDate)
).collect(Collectors.toList());