Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
341 views
in Technique[技术] by (71.8m points)

java - Hibernate FetchMode.JOIN with one to many mapping

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?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Maybe you use Set instead of List @OneToMany(mappedBy = "invoiceHeader", fetch = FetchType.EAGER) @Fetch(value = FetchMode.JOIN) private Set items = new HashSet<>();


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...