3 Replies Latest reply on Nov 24, 2006 9:07 AM by kimbaltrue

    Problem with FetchType.EAGER - multiple instances of an obje

    Reuben Fisher Newbie

      I have a class named Item that contains a list of objects of type Algorithm. The list is defined with the following property and mapping:

      @ManyToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER, targetEntity=Algorithm.class)
      @JoinTable(
      name="item_algorithms",
      joinColumns = { @JoinColumn( name="item_id") },
      inverseJoinColumns = @JoinColumn( name="algorithm_id")
      )
      public List getAlgorithms() {
      return algorithms;
      }

      I have a unit test that creates an Item with three Algorithms and then retrieves the Item with its Identifier

      List itemIDs = new ArrayList(1);
      itemIDs.add("testID");

      Criteria criteria = session.createCriteria(Item.class);
      criteria.add(Restrictions.in("itemIdentifier", idValues));
      List entities = criteria.list();

      System.out.println(entities.size());

      With fetch=FetchType.EAGER - the returned list has 3 items
      With fetch=FetchType.LAZY - the returned list has 1 item

      The retrieved list should return only 1 item. The result should not be different based on the fetch strategy. It looks as if the query in the EAGER case does an outer join yielding 3 records and then creates 3 Items.

      Can anyone please explain what I may be doing wrong?