繁体   English   中英

如何在 spring 引导中由 @OneToMany 注释映射的列上使用 JPA findBy

[英]How to use JPA findBy on column mapped by @OneToMany annotation in spring boot

我有这个 model 调用发票,它包含一个@one to many annotation

这是我的 model:

    public class Invoice {
    
        @Id
        @GeneratedValue(strategy= GenerationType.IDENTITY)
        private int id;
        @Column(name = "serial_number")
        private long serialNumber;
        @Column(name = "status")
        private String status;
        @Column(name = "created_date")
        private Timestamp createdDate;
        @Column(name = "is_deleted")
        private boolean isDeleted;
        @ManyToOne
        @JsonIgnore
        @JoinColumn(name = "customer_id")
        private Customer customer;
        @ManyToOne
        @JoinColumn(name = "employee_id")
        private Employee employee;
        @OneToMany(fetch=FetchType.EAGER, mappedBy="invoice")
        private Set <InvoiceHistory> invoiceHistories;
        @OneToMany (mappedBy = "invoice")
        private Set <InvoiceItem> quantity; 
}

如何通过所有具有员工 id = xxxxx 的发票找到?

使用自定义查询获取发票

Query("select x from  Invoice  x  where x.employee.employeeId=:empId")
List<Invoice > getAllInvoicesByEmpId(Long empId) ;

或者,您可以通过基于员工 Model 中的 OneToMany 关系的 id 加载员工来获取属于员工但来自员工本身的所有发票,但这种方式“不推荐”

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM