[jboss-user] [Persistence, JBoss/CMP, Hibernate, Database] - Join Fetch returning cartesian product

darrenclarke do-not-reply at jboss.com
Wed Aug 16 12:19:47 EDT 2006


Apologies in advance if I'm asking something dumb here; I'm sure there's an obvious answer to this but I've just spent a day with my EJB3 book and on Google and haven't been able to find it.

Imagine I have two entities - Parent and Child.  Parent has a unidirectional one-to-many (lazy) relationship with Child, e.g:

    @Entity
    public class Parent {
        @Id
        private int id;

        @OneToMany( cascade=CascadeType.ALL, fetch=FetchType.LAZY)
        private List children = new ArrayList();

        //snip
    }


    @Entity
    public class Child {
        @Id
        private int id;

        //snip
    }

[Note : my List and ArrayList in Parent are typed as containing Child objects but the syntax isn't being displayed here as it thinks it's XML.]


Now imagine I have two Parents, each with three Child objects and I want a query to return all Parent objects including nested children: 

  SELECT p FROM Parent p LEFT JOIN FETCH p.children

I would expect this to return two Parent objects, but it returns six - each Parent object is repeated n times where n is the number of nested Child objects.  If I use a "SELECT DISTINCT ..." then the problem is fixed, but that seems a strange thing to need to do and I'm wondering if it's necessary, or whether I'm missing something?

Thanks in advance,
Darren

View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=3965538#3965538

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=3965538




More information about the jboss-user mailing list