1 Reply Latest reply on Apr 3, 2008 4:56 AM by mars1412

    @OneToOne and SELECT (1+n) problem

    lupson Newbie

      Lots of searching in various resources havn't solved this issue for me so here goes..:

      Environment:
      JBoss 4.0.5.GA
      Seam 1.2.1
      Oracle 10 running ojdbc14.jar

      Basically, I have two entities, let's call them PERSON and PERSON_METADATA. They have a bi-directional @OneToOne relation.

      Problem: I have 1000 PERSONs and I issue a JPA/HQL query such as: entityManager.createQuery("SELECT p FROM Person p").getResultList();

      Looking at the SQL debug output in the log, this simple JPA query performs 1001 SELECTs instead of performing a JOIN (or LEFT JOIN if optional=true).

      I've fiddled around with various FetchType.EAGER/LAZY, @LazyToOne, @Fetch(FetchMode.EAGER) etc. settings, but still I always get this 1+n number of queries, regardless of how I annotate the @OneToOne relation.

      Any ideas on how to get JPA/Hibernate to EAGER load this using a single JOIN?

      Thanks!

        • 1. Re: @OneToOne and SELECT (1+n) problem
          mars1412 Apprentice

          AFAIK all your fancy annotation stuff is useless when you explicitly code your query.

          if you would just use em.find(Person.class, Id) your annotation setting should work

          but if you hardcode your query, you must explicitly use a fetch statement like:

          from Person p inner join fetch p.metadata