Upload
-
View
6.207
Download
2
Embed Size (px)
Citation preview
영속성 컨텍스트로 보는 JPA
2015.12.12이경원 (woniper)
목차JPA 소개
JDBC 부터 JPA까지 영속성 컨텍스트
참고자료
JPA(Java Persistence API) 소개
Java ORM(Object-Relational Mapping) 표준 F/W
RDB 를 객체로 표현 (매핑 ), 사용Hibernate, Eclipse Link, TopLink Essensials 구현체
JDBC 부터 JPA 까지
JDBC
Connection
Query 생성
Resource Close
xBatis
<select id="select" parameterType="java.lang.Integer" resultType=“User”>
select userId, username, password from User where userId = #{userId}
</select>
Query 작성 지겹지 않나요 ?
JPA
EntityManager em = entityManagerFactory.createEntityManager();
1. Insert
em.persist(user);
2. Select
User user = em.find(User.class, 1);
3. Update
user.setUsername("update Name");user.setPassword("1111");
4. Delete
em.remove(user);
Connection, close
Query 자동 생성 및 실행
데이터 자동 매핑
엔티티 (Entity) 생명주기
@Entity@Table(name = "User")public class User {
@Id @GeneratedValueprivate Integer userId;
@Column(name = "username", nullable = false)private String username;
@Column(name = "password", nullable = false)private String password;
@Column(name = "nickName", nullable = true)private String nickName;
}
USER
비영속 (NEW)
USER
영속 (Managed)
영속성 컨텍스트persist()
USER
삭제 (Removed)
remove()
USER
준영속 (Detached)
detach()
영속성 컨텍스트
이점
쓰기 지연 SQL(SQL 저장소 )
자동 변경 감지1 차 캐시
엔티티동일성 지연로딩
EntityManagerFactory emf = Persistence.createEntityManagerFactory(“persistence");
1 차 캐시 영역@ID Entity Snapshot
SQL 저장소EntityManager em1 = emf.createEntityManager();
em1
1 차 캐시 영역@ID Entity Snapshot
SQL 저장소
EntityManager em2 = emf.createEntityManager();
em2
1 차 캐시 영역@ID Entity Snapshot
1 User User
SQL 저장소 영속성 컨텍스트
JDBC
영속성 컨텍스트는 어떻게 동작할까 ?
입력 (persist)
EntityManager em = emf.createEntityManager();em.getTransaction().begin();
// 비 영속 상태User user = new User("wons", "12345", "woniper");
// 영속 상태// 1 차 캐시 저장 , Query 생성 및 저장em.persist(user);
// 준영속 상태// SQL 저장소 Query DB 에 반영em.getTransaction().commit();em.close();
조회 (find)
EntityManager em = emf.createEntityManager();em.getTransaction().begin();
// 영속 엔티티User user = em.find(User.class, 1);
// 준영속 상태em.getTransaction().commit();em.close();
수정 ( 자동 변경 감지 )
EntityManager em = emf.createEntityManager();em.getTransaction().begin();
// 영속 상태// 1 차 캐시 저장User user = em.find(User.class, 1);
// 자동 변경 감지user.setUserName("updateName");user.setPassword("1111");user.setNickName("updateNick");
// 준영속 상태em.getTransaction().commit();em.close();
삭제 (remove)
EntityManager em = emf.createEntityManager();em.getTransaction().begin();
// 영속상태// 1 차캐시저장User user = em.find(User.class, 1);
// 삭제상태 , Query 생성및저장em.remove(user);
// SQL 저장소 Query DB 에반영em.getTransaction().commit();em.close();
트랜잭션 단위에서 동작commit() 호출 전 flush()
merge
준 영속 엔티티 -> 영속 엔티티
merge 를 알아보기 전에
영속 엔티티 -> 준 영속 엔티티
em.clear();
em.detach(user);
em.close();
EntityManager em = emf.createEntityManager();em.getTransaction().begin();
// 1. 영속 상태 , username == woniperUser user1 = em.find(User.class, 1);
// 2. 준영속 상태em.detach(user1);
// 3. username 속성 변경user.setUserName(“lee-kyung-won");
// 4. 영속 상태em.merge(user1);
em.getTransaction().commit();em.close();
EntityManager em = emf.createEntityManager();em.getTransaction().begin();
// 5. username 속성 값은 ?User user2 = em.find(User.class, 1);
em.getTransaction().commit();em.close();
user2 에 username 은lee-kyung-won
속성이 변경된 준영속 엔티티 merge :
update
비영속 엔티티 merge : insert
참고자료
http://www.tutorialspoint.com/jpa/
http://www.objectdb.com/
https://en.wikibooks.org/wiki/Java_Persistence
http://goo.gl/xzpTdK
https://goo.gl/sqmO9p
https://goo.gl/GhsI4Q
https://goo.gl/GpQzeL
감사합니다 .