简体   繁体   English

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

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

I have this model which call invoice, it's contains a @one to many annotation我有这个 model 调用发票,它包含一个@one to many annotation

this is my model:这是我的 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; 
}

How can i find by all invoices which have the employee id = xxxxx?如何通过所有具有员工 id = xxxxx 的发票找到?

use custom query to get invoices使用自定义查询获取发票

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

Or you can get all invoices belonging to the employee but from the employee itself by loading the employee By id based on OneToMany Relation in Employee Model but this way "Not Recommended "或者,您可以通过基于员工 Model 中的 OneToMany 关系的 id 加载员工来获取属于员工但来自员工本身的所有发票,但这种方式“不推荐”

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

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