关联链接的查询方法
Query method on association links
我有两个实体 User 和 Expense。
费用
@Entity
@Table(name="Expenses")
public class Expense {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String name;
private BigDecimal amount;
private Date date;
@ManyToOne
@JoinColumn(name="category_id")
private Category category;
@ManyToOne
@JoinColumn(name="mode_id")
private Mode mode;
@ManyToOne
@JoinColumn(name="user_id")
private User user;
// Getter setter and constructor
}
用户
@Entity
@Table(name="Users")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String username;
private String password;
private String email;
private boolean enabled;
private String firstName;
private String lastName;
private String phoneNumber;
private String country;
@OneToMany(mappedBy = "user", fetch = FetchType.EAGER, cascade = CascadeType.REMOVE, orphanRemoval = true)
private Set<Expense> expense;
@OneToMany(mappedBy = "user", fetch = FetchType.EAGER, cascade = CascadeType.REMOVE, orphanRemoval = true)
private Set<Mode> mode;
@OneToMany(mappedBy = "user", fetch = FetchType.EAGER, cascade = CascadeType.REMOVE, orphanRemoval = true)
private Set<Category> category;
// Getter setter and constructor
}
费用库
public interface ExpenseRepository extends CrudRepository<Expense, Integer> {
List<Expense> findByDateBetween(@DateTimeFormat(pattern = "dd-MM-yyyy")@Param("startDate") Date startDate, @DateTimeFormat(pattern = "dd-MM-yyyy") @Param("endDate")Date endDate);
}
用户存储库
public interface UserRepository extends CrudRepository<User, Integer>{
Optional<User> findByUsername(String username);
}
注意:我正在使用 Spring 数据休息。
我可以访问用户 1 的费用清单:http://localhost:8080/api/users/1/expense
我还可以通过以下方式获取日期范围内的费用清单:http://localhost:8080/api/expenses/search/findByDateBetween?startDate=01-02-2018&endDate=28-02-2018
我想要实现的是通过为用户使用 'findByDateBetween' 方法来访问费用列表。
这个 Url 不工作并抛出 404 错误。
{
"timestamp": 1517645444489,
"status": 404,
"error": "Not Found",
"message": "No message available",
"path": "/api/users/1/expense/search/findByDateBetween"
}
我做错了什么或者有更好的方法吗?
日期是支出中的一个字段,而不是用户中的一个字段。像这样创建另一个方法
public interface ExpenseRepository extends CrudRepository<Expense, Integer> {
List<Expense> findByUser_IdAndDateBetween(Integer id, @DateTimeFormat(pattern = "dd-MM-yyyy")@Param("startDate") Date startDate, @DateTimeFormat(pattern = "dd-MM-yyyy") @Param("endDate")Date endDate);
}
并使用
调用它
我有两个实体 User 和 Expense。
费用
@Entity
@Table(name="Expenses")
public class Expense {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String name;
private BigDecimal amount;
private Date date;
@ManyToOne
@JoinColumn(name="category_id")
private Category category;
@ManyToOne
@JoinColumn(name="mode_id")
private Mode mode;
@ManyToOne
@JoinColumn(name="user_id")
private User user;
// Getter setter and constructor
}
用户
@Entity
@Table(name="Users")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String username;
private String password;
private String email;
private boolean enabled;
private String firstName;
private String lastName;
private String phoneNumber;
private String country;
@OneToMany(mappedBy = "user", fetch = FetchType.EAGER, cascade = CascadeType.REMOVE, orphanRemoval = true)
private Set<Expense> expense;
@OneToMany(mappedBy = "user", fetch = FetchType.EAGER, cascade = CascadeType.REMOVE, orphanRemoval = true)
private Set<Mode> mode;
@OneToMany(mappedBy = "user", fetch = FetchType.EAGER, cascade = CascadeType.REMOVE, orphanRemoval = true)
private Set<Category> category;
// Getter setter and constructor
}
费用库
public interface ExpenseRepository extends CrudRepository<Expense, Integer> {
List<Expense> findByDateBetween(@DateTimeFormat(pattern = "dd-MM-yyyy")@Param("startDate") Date startDate, @DateTimeFormat(pattern = "dd-MM-yyyy") @Param("endDate")Date endDate);
}
用户存储库
public interface UserRepository extends CrudRepository<User, Integer>{
Optional<User> findByUsername(String username);
}
注意:我正在使用 Spring 数据休息。
我可以访问用户 1 的费用清单:http://localhost:8080/api/users/1/expense
我还可以通过以下方式获取日期范围内的费用清单:http://localhost:8080/api/expenses/search/findByDateBetween?startDate=01-02-2018&endDate=28-02-2018
我想要实现的是通过为用户使用 'findByDateBetween' 方法来访问费用列表。
这个 Url 不工作并抛出 404 错误。
{
"timestamp": 1517645444489,
"status": 404,
"error": "Not Found",
"message": "No message available",
"path": "/api/users/1/expense/search/findByDateBetween"
}
我做错了什么或者有更好的方法吗?
日期是支出中的一个字段,而不是用户中的一个字段。像这样创建另一个方法
public interface ExpenseRepository extends CrudRepository<Expense, Integer> {
List<Expense> findByUser_IdAndDateBetween(Integer id, @DateTimeFormat(pattern = "dd-MM-yyyy")@Param("startDate") Date startDate, @DateTimeFormat(pattern = "dd-MM-yyyy") @Param("endDate")Date endDate);
}
并使用
调用它