public class JpaMain { public static void main(String[] ars) { EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("hello"); EntityManager em = entityManagerFactory.createEntityManager(); EntityTransaction transaction = em.getTransaction();
transaction.begin(); try { Member member = new Member(); member.setUsername("hello"); em.persist(member); em.flush(); em.clear();
Member savedMember = em.find(Member.class, 1L); printMemberAndTeam(savedMember);
transaction.commit(); } catch (Exception e) { transaction.rollback(); } finally { em.close(); } entityManagerFactory.close(); }
private static void printMember(Member member){ System.out.println("member = " + member.getUsername()); }
private static void printMemberAndTeam(Member member) { String username = member.getUsername(); System.out.println("username = " + username);
Team team = member.getTeam(); System.out.println("team = " + team.getName()); } }
|