I'm using the criteria query to fetch Customers.
public class PurchaseOrder { ..... ..... @ManyToOne(fetch=FetchType.EAGER, optional=true) @JoinColumn(name="ReportingCustomer_ID", nullable=true, insertable=false, updatable=false) @Fetch(FetchMode.JOIN) @NotFound(action=NotFoundAction.IGNORE) public ReportingCustomer getReportingCustomer() { return reportingCustomer; } }
While getting PurchaseOrder it does a LEFT OUTER JOIN as below
select ... from PurchaseOrder this_ left outer join ReportingCustomer reportingc2_ on this_.ReportingCustomer_ID=reportingc2_.ReportingCustomer_ID where ...
- When there is an entry in ReportingCustomer - It fires only the above query.
- When there is no entry for that record in ReportingCustomer - It fires a query for each PURCHASEORDER (m+1) queries.
I use "Progress" driver to connect to DB. I'm not sure why it fires m+1 queries only in scenario 2.
SELECTstatements for a@ManyToOnerelation withEAGERfetch type when the requested fetch mode isJOIN. The accepted answer states to remove thefetch=FetchType.EAGERfrom the annotation, an action that won't change anything sinceEAGERis the default fetch type for the annotation; even if you interpret the answer as "changeEAGERwithLAZY", that doesn't explain why Hibernate issuesSELECTstatements when it's instructed not to do so.