Hibernate Fetch Join Elections

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

@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;
   }

      

Information about the child may not be available. I want the Parent object to always return and for the Child object to be initialized if relevant information is available.

I am trying 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 generated by this HQL it does a separate SELECT for each child after the original SELECT.

Can anyone point me to why this is happening to determine that the FetchMode is set to JOIN and has the fetch selection been removed explicitly in the HQL query?

+3


source to share


1 answer


Does your child class have some associations? Please refer to the link below which may help you.



Avoid n + 1 impatient sampling of collection items association for children

0


source







All Articles