λ³Έ λ ν¬μ§ν 리λ κΉμνμ μ€νλ§ λΆνΈμ JPA μ€λ¬΄ μμ μ 볡 λ‘λ맡μ μμ²νλ©° μμ±λμ΄μ§κ³ μμ΅λλ€.
λ§ν¬ : https://www.inflearn.com/roadmaps/149
public class JpaStartMain {
public static void main(String[] args) {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("hello"); // 1. μν°ν° 맀λμ ν©ν 리. νλλ§ μμ±ν΄μ μ ν리μΌμ΄μ
μ 체μμ 곡μ
EntityManager em = emf.createEntityManager(); // 2. μν°ν° 맀λμ . μ°λ λ κ°μ 곡μ X ( μ¬μ©νκ³ λ²λ €μΌ νλ€. )
EntityTransaction tx = em.getTransaction(); // 3. νΈλμμ
. JPA μ λͺ¨λ λ°μ΄ν° λ³κ²½μ νΈλμμ
μμμ μ€νλμ΄μΌ νλ€.
tx.begin(); // 3-1. νΈλμμ
μμ
try {
// CRUD.. Code.. etc...
tx.commit(); // νΈλμμ
μ μμ 컀λ°(λ°μ)
}catch (Exception e){
tx.rollback(); // νΈλμμ
λ¬Έμ λ°μμ λ‘€λ°±
}finally {
em.close(); // μν°ν° 맀λμ λ κΌ λ«μμ£Όμ΄μΌν¨.
}
emf.close();
}
}
...
tx.begin();
try {
// INSERT
MemberVO insertMember = new MemberVO;
insertMember.setId("1L");
insertMember.setName("name");
entityManager.persist(insertMember);
// READ
MemberVO readMember = entityManager.find((Mamber.class, 1L);
// DELETE
MemberVO deleteMember = entityManager.find((Mamber.class, 1L);
entityManager.remove(deleteMember);
// UPDATE
MemberVO updateMember = entityManager.find((Mamber.class, 1L);
updateMember.setName("changeName");
tx.commit();
}catch (Exception e){
tx.rollback();
}finally {
entityManager.close();
}