Enterprise Technologies

NAVIGATION
CATEGORIES
REFERRENCE
LINKS
  • LEFT JOIN FETCH - trouble understanding it

    0 answers - 1543 bytes - related search similar search Add To My Delicious Add To My Stumble Upon Add To My Google Mark Add To My Facebook Add To My Digg Add To My Reddit

    Hi,

    I've been reading the JPA spec lately and have come across a trouble understanding how LEFT JOIN FETCH is supposed to work.

    Let's assume I have two entities - Person and Project. Let's assume that there's a @ManyToMany association between Person and Project where the attribute - projects - of Person points to the Projects the person is involved into.

    Provided that there're 2 people (2 Person entities) and 3 projects in a database where only the person X is assigned to 3 projects.

    The following query will return 4 Person entities whereas only 2 are unique (distinct).

    Query query = em

    .createQuery("SELECT DISTINCT p FROM Person p LEFT JOIN FETCH p.projects");

    List<Person> people = query.getResultList();

    assert people.size() == 4;

    Set<Person> peopleSet = new LinkedHashSet(people);

    assert peopleSet.size() == 2;

    Why am I getting 4? Shouldn't a JPA provider return only 2, esp. when DISTINCT is used? Or if it's a default behaviour to return 4, how to filter out the others unneeded? Is there a JPA property? Is java.util.Set the only solution to it? What about the penalty of instantiating each and every Person instance when only 2 are really of use?

    Just for the record, I'm using Apache OpenJPA 0.9.7-SNAPSHOT and Apache Derby.

    Jacek

    --

    Jacek Laskowski

    http://www.JacekLaskowski.pl

Re: LEFT JOIN FETCH - trouble understanding it


max 4000 letters.
Your nickname that display:
In order to stop the spam: 0 + 9 =
QUESTION ON "Enterprise Technologies"

JAVA TECH