title: How to make JOIN in JPA?
body: <p>My application with JPA (Hibernate) and Spring. My enitites are:</p>
  
  <p><strong>Department</strong></p>
  
  <pre><code>@Entity
  @Table(schema = "myschema")
  public class Department {
  
      @Id
      @GeneratedValue(generator = "uuid")
      @GenericGenerator(name = "uuid", strategy = "uuid2")
      @Column(columnDefinition = "BINARY(16)", length = 16 )
      private UUID uuid;
  
      @Column(name = "name", length = 200, nullable = false)
      private String name;
  
      @OneToMany(fetch = FetchType.LAZY, mappedBy = "department", cascade = CascadeType.ALL, orphanRemoval = true)
      List&lt;User&gt; users = new ArrayList&lt;&gt;();
  }
  </code></pre>
  
  <p><strong>User</strong></p>
  
  <pre><code>@Entity
  @Table(schema = "myschema")
  public class User {
  
      @Id
      @GeneratedValue(generator = "uuid")
      @GenericGenerator(name = "uuid", strategy = "uuid2")
      @Column(columnDefinition = "BINARY(16)", length = 16 )
      private UUID uuid;
  
      @NotNull
      @Column(name = "login", length = 100, nullable = false)
      private String login;
  
      @ManyToOne
      @JoinColumn(name = "department_fk", nullable = false)
      private Department department;
  
  }
  </code></pre>
  
  <p><strong>DAO</strong> for Department:</p>
  
  <pre><code>@Repository("departmentDao")
  public class DepartmentDao {
  
      @PersistenceContext
      private EntityManager entityManager;
  
      @Transactional
      public UUID save(Department entity) {
          log.info("Department entity will be persisted", entity);
          entityManager.persist(entity);
          return entity.getUuid();
      }
  
      public List&lt;Department&gt; getAllWithUsers() {
          log.info("Select all departments");
          CriteriaQuery&lt;Department&gt; criteria = entityManager.getCriteriaBuilder().createQuery(Department.class);
          criteria.from(Department.class).fetch("users");
          return entityManager.createQuery(criteria).getResultList();
      }
  }
  </code></pre>
  
  <p>Whan I run the code in Main class it joins the entities (<strong>but duplicates them, why?</strong>):</p>
  
  <p><strong>Main</strong></p>
  
  <pre><code>ApplicationContext context = new ClassPathXmlApplicationContext("META-INF/spring.xml");
  UserDao userDao = (UserDao) context.getBean("userDao");
  DepartmentDao departmentDao = (DepartmentDao) context.getBean("departmentDao");
  
  Department newDep = new Department("WebDepartment", new Date());
  departmentDao.save(newDep);
  
  User newUser1 = new User("Test1", newDep);
  User newUser2 = new User("Test2", newDep);
  userDao.save(newUser1);
  userDao.save(newUser2);
  
  List&lt;Department&gt; deps = departmentDao.getAllWithUsers();
  
  //deps
  //[Department{name='WebDepartment', users = ['Test1', 'Test2']}, 
  // Department{name='WebDepartment', users = ['Test1', 'Test2']}]
  </code></pre>
  
  <p>Whan I run  a unit test with the same code, list of departments still contains two entities, each of department has empty list of users:</p>
  
  <pre><code>@ContextConfiguration(locations = "classpath:META-INF/spring-test.xml")
  @RunWith(SpringJUnit4ClassRunner.class)
  public class DaoTests {
  
      @Autowired
      private DepartmentDao departmentDao;
  
      @Autowired
      private UserDao userDao;
  
      @Test
      @Transactional
      public void daoTests() throws NoSuchAlgorithmException {
  
          Department newDep = new Department("WebDepartment", new Date());
          departmentDao.save(newDep);
  
          User newUser1 = new User("Test1", newDep);
          User newUser2 = new User("Test2", newDep);
          userDao.save(newUser1);
          userDao.save(newUser2);
          List&lt;Department&gt; deps = departmentDao.getAllWithUsers();
  
          //deps
          //[Department{name='WebDepartment', users = []}, 
          // Department{name='WebDepartment', users = []}]
      }
  }
  </code></pre>
  
  <p>So, I have two questions. 
  1. Why I get two Department-entites?
  2. Why <strong>join</strong> works in Main, but in test it doesn't?</p>