public class JpaMain {
public static void main(String[] args) {
System.out.println(1111);
EntityManagerFactory emf = Persistence.createEntityManagerFactory("hello");
EntityManager em = emf.createEntityManager();
EntityTransaction tx = em.getTransaction();
tx.begin();
try {
Member findMember = em.find(Member.class, 1L);
findMember.setName("member3");
tx.commit();
} catch (Exception e) {
tx.rollback();
} finally {
em.close();
}
emf.close();
}
}
- em.persist(member)๋ฅผ ํ์ง ์์์์๋update์ฟผ๋ฆฌ๊ฐ ๋๊ฐ๋ค.
- ๊ทธ ์ด์ ๋ jpa๋ฅผ ํตํด์ entity๋ฅผ ๊ฐ์ ธ์ค๋ฉด jpa๊ฐ ๊ด๋ฆฌํ๋ค. ๊ทธ๋ ๊ธฐ ๋๋ฌธ์, ํธ๋์ญ์ ์ปค๋ฐ ์์ ์ jpa๊ฐ ๋ณ๊ฒฝ์ฌํญ์ด ์๋์ง๋ฅผ ํ์ธ(๋ณ๊ฒฝ ๊ฐ์ง)ํ๊ณ update ์ฟผ๋ฆฌ๋ฅผ ์งํํ๋ ๊ฒ์ด๋ค.
- JPA์์ ๋ฐ์ดํฐ ๋ณ๊ฒฝ์ ํธ๋์ญ์ ์์์ ์คํ
1. ์กฐํ๋ฐฉ๋ฒ
EntityManager.find()
2. ๋ณต์กํ ์กฐํ๋???
JPQL : ๊ฐ์ฒด ์งํฅ ์ฟผ๋ฆฌ ์ธ์ด
- ์ํฐํฐ ๊ฐ์ฒด๋ฅผ ๋์์ผ๋ก ์ฟผ๋ฆฌ๋ฅผ ์์ฑ(ํ ์ด๋ธ์ ๋์์ผ๋ก ํ๋ ๊ฒ์ด ์๋)
List<Member> resultList = em.createQuery("select m from Member as m", Member.class).getResultList(); for (Member member : resultList) { System.out.println("member.name = " + member.getName()); }
Leave a comment