spring-boot - Spring数据JPA中按时间戳排序
问题描述
我正在尝试从Products
我在findByIdOrderByTransactionDateDesc()
Spring Boot 应用程序中使用 Spring Data JPA 的表中获取数据。由于我的交易日期是时间戳类型,我无法获得最新时间戳的获取记录的正确顺序。
findByIdOrderByProductsDetails_DateDesc();
我的产品类是:
@Entity
public class Products {
@Id
private Integer id;
private float price;
@JsonIgnore
@OneToMany(mappedBy = "product", cascade = CascadeType.ALL)
private List<ProductDetails> productsDetails;}
和ProductDetails类是:
@Entity
public class ProductDetails implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Column(name = "transaction_date")
private Timestamp transactionDate;
@ManyToOne
@JoinColumn(name = "product_id")
@JsonBackReference
private Products product;
}
解决方案
productsDetails 是 OneToMany 关系,您无法使用 Spring Data 存储库方法进行导航。
您必须使用查询:
@Query("select p from Products join productsDetails d where p.id = :id order by d.transactionDate desc")
findByIdOrderByProductsDetails_DateDesc(Integer id);
推荐阅读
- c# - 如何将 out 参数用于二维数组中的结构条目?
- css - 选择特定标签特定深度的元素
- ios - iOS 14 UIPickerView 所选行的颜色
- java - IntelliJ Jetbrains 语言注释 @Language("SQL") 不起作用,但 @Language("sql") 起作用
- spring - 如何在 TABLE batch_step_context 的 COLUMN(可能是 FIELD)short_context 中添加额外信息?
- graphql - 当创建突变具有必填字段但更新没有时,如何重用此输入类型?
- java - 如何编写一个以类为参数的方法
- database - 如何记录员工每个月在两个实体之间的 ER 图中工作的小时数?
- visual-studio-code - VS Code Notebook - 可以自定义配色方案吗?
- docker - 无法在 Windows 10 上启动 docker