Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
159 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?

question from:https://stackoverflow.com/questions/65600608/hibernate-fetchmode-join-with-one-to-many-mapping

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

1 Reply

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<>();


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...