title: JPA - EntityManager find method returns duplicates
body: <p>I have entity with two Lists of children entities. When calling EntityManager.find() it returns duplicates based on multiplication of two lists.</p>
  
  <p>I'm using Spring, Jackson and JPA with Hibernate and SQL Server.</p>
  
  <p>When testing with parent that has <strong>N</strong> elements of first and <strong>M</strong> of second child entity it always returns <strong>N*M</strong> elements of both entities.</p>
  
  <p>For example below there are <strong>3</strong> <em>tasks</em> and <strong>5</strong> <em>comments</em> and JPA returns <strong>15</strong> for both lists. (5 copies of task list, and 3 copies of comment list)</p>
  
  <p>Output from controller is:</p>
  
  <pre><code>Comments 15
  Tasks 15
  </code></pre>
  
  <p>And the rest of the code is below.</p>
  
  <p><strong>controller.java</strong></p>
  
  <pre><code>@RequestMapping(method = RequestMethod.GET)
  public String listAll(Model model) {
  
      Goal goal = new Goal();
  
      goal = service.getGoalById(25);
  
      System.out.println("Comments " + goal.getComments().size());
      System.out.println("Tasks " + goal.getTasks().size());
  
      return "home";
  }
  </code></pre>
  
  <p><strong>service.java</strong></p>
  
  <pre><code>@Transactional
  public Goal getGoalById(int goalId) {
      Goal goal = em.find(Goal.class, goalId);
  
      return goal;
  }
  </code></pre>
  
  <p><strong>goal.java</strong></p>
  
  <pre><code>@Entity
  @Table(name = "goal")
  public class Goal {
  
  @Id
  @GeneratedValue
  private int id;
  
  @JsonManagedReference
  @OneToMany(mappedBy = "tasksGoal", cascade = CascadeType.PERSIST, fetch = FetchType.EAGER)
  private List&lt;Task&gt; projectTasks = new ArrayList&lt;Task&gt;();
  
  @JsonManagedReference
  @OneToMany(mappedBy = "commentsGoal", cascade = CascadeType.PERSIST, fetch = FetchType.EAGER)
  private List&lt;Comment&gt; goalComments = new ArrayList&lt;Comment&gt;();
  
  ...
  
  }
  </code></pre>
  
  <p><strong>task.java</strong></p>
  
  <pre><code>@Entity
  @Table(name="projectTask")
  public class Task {
  
  @Id
  @GeneratedValue
  private int id;
  
  @JsonBackReference
  @ManyToOne (fetch = FetchType.LAZY)
  @JoinColumn(name = "task_goal_id")
  private Goal tasksGoal;
  
  ...
  
  }
  </code></pre>
  
  <p><strong>comment.java</strong></p>
  
  <pre><code>@Entity
  @Table (name="goalComment")
  public class Comment {
  
  @Id
  @GeneratedValue
  private int id;
  
  @JsonBackReference
  @ManyToOne (fetch = FetchType.LAZY)
  @JoinColumn(name = "goal_id")
  private Goal commentsGoal;
  
  ...
  
  }
  </code></pre>
  
  <p><strong>persistence.xml</strong></p>
  
  <pre><code>&lt;persistence xmlns="http://java.sun.com/xml/ns/persistence"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0"
  xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"&gt;
  &lt;persistence-unit name="persistenceUnit"
      transaction-type="RESOURCE_LOCAL"&gt;
      &lt;provider&gt;org.hibernate.ejb.HibernatePersistence&lt;/provider&gt;
      &lt;properties&gt;
          &lt;property name="hibernate.dialect" value="org.hibernate.dialect.SQLServer2008Dialect" /&gt;
          &lt;property name="hibernate.hbm2ddl.auto" value="update" /&gt;
          &lt;property name="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.ImprovedNamingStrategy" /&gt;
          &lt;property name="hibernate.connection.charSet" value="UTF-8" /&gt;
  
          &lt;!-- Hibernate prints SQL --&gt;
          &lt;property name="hibernate.show_sql" value="true" /&gt;
  
      &lt;/properties&gt;
  &lt;/persistence-unit&gt;
  </code></pre>
  
  <p></p>