There are two entity InvoiceHeader and InvoiceDetail with one to many mapping as below.
InvoiceHeader
@Entity
@Table(name = "view_invoice_hdr")
@IdClass(InvoiceHdrPK.class)
@Getter
@NoArgsConstructor
@EqualsAndHashCode
public class InvoiceHeader{
....
@OneToMany(mappedBy = "invoiceHeader", fetch = FetchType.EAGER)
@Fetch(value = FetchMode.JOIN)
private List<InvoiceDetail> items;
}
InvoiceHdrPK
@Getter
@ToString
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode
public class InvoiceHdrPK implements Serializable {
private String invoiceNumber;
private InvoiceSource invoiceSource;
private InvoiceType invoiceType;
private Long companyId;
private Integer invoiceYear;
}
Similarly InvoiceDetail
@Entity
@Table(name = "view_inv_dtl")
@IdClass(InvoiceDetailPK.class)
@Getter
@NoArgsConstructor
@EqualsAndHashCode
public class InvoiceDetail{
....
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumns(value = {
@JoinColumn(name = "invoice_number", referencedColumnName = "invoice_number", insertable = false, updatable = false),
@JoinColumn(name = "invoice_source", referencedColumnName = "invoice_source", insertable = false, updatable = false),
@JoinColumn(name = "invoice_type", referencedColumnName = "invoice_type", insertable = false, updatable = false),
@JoinColumn(name = "company_id", referencedColumnName = "company_id", insertable = false, updatable = false),
@JoinColumn(name = "invoice_year", referencedColumnName = "invoice_year", insertable = false, updatable = false),
})
private InvoiceHeader invoiceHeader;
}
InvoiceDetailPK
@Getter
@ToString
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode
public class InvoiceDetailPK implements Serializable {
private String invoiceNumber;
private InvoiceSource invoiceSource;
private InvoiceType invoiceType;
private BigDecimal rate;
private Long itemNo;
private Long companyId;
private Integer invoiceYear;
}
Both the entities are not an actual table in db level but are maintained as views.
Using JPA I am trying to fetch invoices using findAll(Specification, pageable) with FetchMode.JOIN to avoid n+1 query problem.
But getting exception
org.hibernate.QueryException: query specified join fetching, but the owner of the fetched association was not present in the select list...
So how can I fix this and what am I missing?
question from:
https://stackoverflow.com/questions/65600608/hibernate-fetchmode-join-with-one-to-many-mapping 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…