3

I have a Hibernate Parent object with a Child OneToOne relation:

@OneToOne
@Fetch(FetchMode.JOIN)
@NotFound(action=NotFoundAction.IGNORE)
@JoinColumns({
    @JoinColumn(name="column_a", referencedColumnName="column_a", insertable = false, updatable = false),
    @JoinColumn(name="column_b", referencedColumnName="column_b", insertable = false, updatable = false),
    @JoinColumn(name="column_c", referencedColumnName="column_c", insertable = false, updatable = false)
   })
   public Child getChild()
   {
      return child;
   }

The Child information may not always be available. I want the Parent object to always be returned and for the Child object to be initialized if the associated information is available.

I attempt to use the following HQL to retrieve a List of Parent objects:

String queryString = "select parent from Parent parent"
                  + " left join fetch parent.child child "
                  + " where parent <.... meets criteria> "
EntityManager em = getEntityManager();
Query query = em.createQuery(queryString);
query.setParameter(<set parameters...>);
query.getResultList();

When I look at the SQL produced by this HQL it is performing a separate SELECT for each Child after the original SELECT.

Can anyone point me as to why this is happening desipte the FetchMode being set to JOIN and left join fetch explicitly being set in the HQL query?

SamF
  • 255
  • 1
  • 3
  • 16

1 Answers1

0

Does your child class also has some associations?? Please refer to below link which might help you .

Avoiding n+1 eager fetching of child collection element association

Community
  • 1
  • 1