在需要使用表中的外键执行查询时出现问题。问题是,在上面的@Query(在上面的ExpenseRepository中)中,我的日志显示他没有按用户id(外键)搜索,而是在搜索ExpenseEntity类的主键。
这是我的用户实体类:
@Entity
public class User implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String email;
@NotBlank(message = "Password required")
@Size(min = 6)
private String password;
//getters and setters
这是我的费用实体类:
@Entity
public class Expenses implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private long limitValue;
private long spentValue;
@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name = "id")
private User user;
//getters and setters here
最后,这是我的存储库,在这里我自己进行查询
public interface ExpensesRepository extends JpaRepository< Expenses, Long> {
@Query("FROM Expenses g where g.id = :userId")
GestaoGastos findAllByCurrentUser(@Param("userId") Long userId);
}
最佳答案
使用@Query
中的字段而不是列
public interface ExpensesRepository extends JpaRepository< Expenses, Long> {
@Query("FROM Expenses g where g.user.id = :userId")
GestaoGastos findAllByCurrentUser(@Param("userId") Long userId);
}