작성
·
463
1
public static void main(String[] args) {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("hello");
EntityManager em = emf.createEntityManager();
EntityTransaction tx = em.getTransaction();
tx.begin();
System.out.println("zzzzzzzzzzzzzzzzzzzzzzzzzzzz");
//비영속
try{
Team teamA = new Team();
teamA.setName("teamA");
em.persist(teamA);
Team teamB = new Team();
teamB.setName("teamB");
em.persist(teamB);
Member member1 = new Member();
member1.setUsername("member1");
member1.setTeam(teamA);
em.persist(member1);
Member member2 = new Member();
member2.setUsername("member2");
member2.setTeam(teamA);
em.persist(member2);
Member member3 = new Member();
member3.setUsername("member3");
member3.setTeam(teamB);
em.persist(member3);
Member member4 = new Member();
member4.setUsername("member4");
member4.setTeam(null);
em.persist(member4);
em.flush();
em.clear();
String query ="select t From Team t join fetch t.members";
List<Team> result = em.createQuery(query, Team.class).getResultList();
for (Team team : result) {
System.out.println("team= " + team.getName() + "|members=" + team.getMembers().size());
}
tx.commit();
} catch (Exception e){
tx.rollback();
e.printStackTrace();
}finally {
em.close();
}
emf.close();
}
이 코드를 실행시켰더니 14분 50초에 나오는 3줄이 나오는 것이 아닌
team= teamA|members=2
team= teamB|members=1
이렇게 나오는데 혹시 어떤 이유에서 그런것인지 궁금합니다.