简体   繁体   中英

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

this is my 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?

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 "

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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