title: Hibernate JPA, duplicate children result?
body: <p>I have some relations like the following:</p>
  
  <pre><code>@Entity
  @Table(name="customer")
  public class Customer
  {
      @Id
      @Column(name="id")
      private int id;
  
  
      @OneToMany(fetch=FetchType.EAGER, mappedBy="customer", cascade=CascadeType.ALL)
      private List&lt;OrderedItem&gt; items;
  
      public List&lt;OrderedItem&gt; getItems ()
      {
          return items;
      }
  
      public void setItems (List&lt;OrderedItem&gt; _items)
      {
          items = _items;
      }
  }
  
  @Entity
  @Table(name="ordereditem")
  @IdClass(OrderedItemPK.class)
  public class OrderedItem
  {
      @Id
      @ManyToOne
      @JoinColumn(name="id_customer")
      private Customer customer;
  
      @Id
      @ManyToOne
      @JoinColumn(name="id_item")
      private Item item;
  
      public Customer getCustomer ()
      {
          return customer;
      }
      public void setCustomer (Customer _cust)
      {
          customer = _cust;
      }
      public Item getItem ()
      {
          return item;
      }
      public void setItem (Item _item)
      {
          item = _item;
      }
  }
  
  @Entity
  @Table(name="item")
  public class Item
  {
      @Id
      @Column(name="id")
      private int id ;
  
      @ManyToMany(fetch=FetchType.EAGER)
      @JoinTable(name="itemcategory",
          joinColumns=
              @JoinColumn(name="id_item", referencedColumnName="id"),
          inverseJoinColumns=
              @JoinColumn(name="id_category", referencedColumnName="id")
      )
      private List&lt;Category&gt; categories ;
  
  
  
      public int getId ()
      {
          return id;
      }
  
      public void setId (int _id)
      {
          id = _id;
      }
  
      public List&lt;Categories&gt; getCategories ()
      {
          return categories;
      }
  
      public void setCategories (List&lt;Category&gt; _categories)
      {
          categories = _categories;
      }
  }
  
  @Entity
  @Table(name="category")
  public class Category
  {
      @Id
      @Column(name="id")
      private int id;
  
      public int getId ()
      {
          return id;
      }
  
      public void setId (int _id)
      {
          id = _id;
      }
  }
  
  public class OrderedItemPK implements Serializable
  {
      private int customer ;
      private int item ;
  
      public int getCustomer ()
      {
          return a;
      }
      public void setCustomer (int _cust)
      {
          customer = _cust;
      }
      public int getItem ()
      {
          return item;
      }
      public void setItem (int _item)
      {
          item = _item;
      }
  
      public int hashCode() {
          return customer + item;
      }
  
      public boolean equals(Object _o) {
          OrderedItemPK other = (OrderedItemPK) _o;
          return (customer == other.customer) &amp;&amp; (item == other.item);
      }
  }
  </code></pre>
  
  <p>The repository:</p>
  
  <pre><code>public interface CustomerRepo extends JpaRepository&lt;Customer, Integer&gt; {
  }
  </code></pre>
  
  <p>the service:</p>
  
  <pre><code>@Service
  @Transactional
  public class CustomerSvc
  {
      @Autowired
      CustomerRepo repo;
  
      public Customer findCustomer(int _id) {
          return repo.findOne(_id);
      }
  
      public List&lt;Customer&gt; findAllCustomer() {
          return repo.findAll();
      }
  }
  </code></pre>
  
  <p>Here is the content in the database:</p>
  
  <pre><code>customer table:
  
  id
  -----
  1
  2
  3
  
  ordereditem table:
  
  id_customer     id_item
  -------------------------
  1                1
  1                2
  1                3
  
  item table:
  
  id
  -----
  1
  2
  3
  
  category table:
  
  id
  ----
  1
  2
  3
  
  itemcategory table:
  
  id_item     id_category
  -----------------------
  1            1
  1            2
  1            3
  2            1
  2            2
  2            3
  3            1
  3            2
  3            3
  </code></pre>
  
  <p>How I use the service:</p>
  
  <pre><code>Customer c = custSvc.findOne(1);
  
  // this prints Number of ordered items: 9
  System.out.println("Number of ordered items: " + c.getItems().size());
  
  List&lt;Customer&gt; custs = custSvc.findAll();
  c = custs.get(0);
  
  // this prints Number of ordered items: 3
  System.out.println("Number of ordered items: " + c.getItems().size());
  </code></pre>
  
  <p>The correct amount should be 3, a customer with the id 1 has 3 OrderedItems, these 3 OrderedItems have 1 Item for each of them and every Item has 3 categories.</p>
  
  <p>I wonder why it only gives the wrong children if I use method like findOne from the Spring Data and find method from the EntityManager, it correctly gives the right children if I use method like findAll or findByAttribute from the Spring Data.</p>