SlideShare a Scribd company logo
1 of 101
Download to read offline
JPA 잘 (하는 척) 하기
SLiPP-JPA 이경원
목차
● JPA 소개
● JDBC부터 JPA까지
● 객체와 테이블
● 엔티티 생명주기
● 영속성 컨텍스트
● Spring Data JPA
● 참고 자료
● QnA
JPA(Java Persistence API) 소개
● Java ORM(Object-Relational Mapping) 표준 F/W
● RDB를 객체로 표현(매핑), 사용
● Hibernate, Eclipse Link, TopLink Essensials 구현체
JDBC부터 JPA까지
요구 사항
단순 CRU(입력, 조회, 수정)
public class User {
private int userId;
private String name;
private String password;
}
JDBC
// connection…
1. 쿼리 생성
String insertQuery = "insert into User(userId, name, password) values(?, ?, ?)";
2. parameter 매핑
pstmt.setInt(1, user.getUserId());
pstmt.setString(2, user.getName());
pstmt.setString(3, user.getPassword());
3. 쿼리 실행
pstmt.executeUpdate(insertQuery);
// close...
// connection…
1. 쿼리 생성
String selectQuery = "select userId, name, password from User where userId =" +
user.getUserId();
2. 쿼리 실행
ResultSet rs = stmt.executeQuery(selectQuery);
3. 데이터 매핑
user.setUserId(rs.getInt("userId"));
user.setName(rs.getString("name"));
user.setPassword(rs.getString("password"));
// close...
// connection…
1. 쿼리 생성
String updateQuery = "update User set name = ?, password = ? where userId = ?";
2. parameter 매핑
pstmt.setString(1, user.getName());
pstmt.setString(2, user.getPassword());
pstmt.setInt(3, user.getUserId());
3. 쿼리 실행
pstmt.executeUpdate(updateQuery);
// close...
Query 생성, 실행, 데이터 매핑 반복
Connection, close 코드 반복
MyBatis
1. insert
<insert id="inser" parameterType="User">
insert into User(userId, name, password) values (#{userId}, #{name}, #{password})
</insert>
2. select
<select id="select" parameterType="java.lang.Integer" resultType="User">
select userId, name, password from User where userId = #{userId}
</select>
3. update
<update id="update" parameterType="User">
update User set name = #{name}, password = #{password} where userId = #{userId}
</update>
Connection, close 위임
parameter 매핑, 데이터 매핑 위임
하지만 여전히 Query 작성 반복
JPA
EntityManager em = entityManagerFactory.createEntityManager();
// Insert
em.persist(user);
// Select
User user = em.find(User.class, user.getUserId());
// Update
user.setName("update Name");
user.setPassword("1111");
// Delete
em.remove(user);
Connection, close 위임
쿼리 자동 생성, 실행
User 테이블에
“nickName” 컬럼이 추가 된다면?
public class User {
private int userId;
private String name;
private String password;
// nickName 컬럼 추가
private String nickName;
}
JDBC
1. insert 쿼리 생성
String insertQuery = "insert into User(userId, name, password, nickName) values(?, ?, ?, ?)";
2. parameter 매핑
pstmt.setInt(1, user.getUserId());
pstmt.setString(2, user.getName());
pstmt.setString(3, user.getPassword());
pstmt.setString(4, user.getNickName());
3. 쿼리 실행
pstmt.executeUpdate(insertQuery);
1. select 쿼리 생성
String selectQuery = "select userId, name, password, nickname from User where userId = " +
user.getUserId();
2. 쿼리 실행
ResultSet rs = stmt.executeQuery(selectQuery);
3. 데이터 매핑
user.setUserId(rs.getInt("userId"));
user.setName(rs.getString("name"));
user.setPassword(rs.getString("password"));
user.setNickName(rs.getString("nickName"));
1. update 쿼리 생성
String updateQuery = "update User set name = ?, password = ?, nickName = ? where userId =
?";
2. parameter 매핑
pstmt.setString(1, user.getName());
pstmt.setString(2, user.getPassword());
pstmt.setString(3, user.getNickName());
pstmt.setInt(4, user.getUserId());
3. 쿼리 실행
pstmt.executeUpdate(updateQuery);
Query 수정, 데이터 매핑 코드 추가
MyBatis
1. insert
<insert id="inser" parameterType="User">
insert into User(userId, name, password, nickName)
values (#{userId}, #{name}, #{password}, #{nickName})
</insert>
2. select
<select id="select" parameterType="java.lang.Integer" resultType="User">
select userId, name, password, nickName from User where userId = #{userId}
</select>
3. update
<update id="update" parameterType="User">
update User set name = #{name}, password = #{password}, nickName = #{nickName}
where userId = #{userId}
</update>
Query 수정
JPA는 얼마나 변경됐을까요?
// insert
em.persist(user);
// select
em.find(User.class, user.getUserId());
// update
user.setName("update Name");
user.setPassword("1111");
// delete
em.remove(user);
// insert
em.persist(user);
// select
em.find(User.class, user.getUserId());
// update
user.setName("update Name");
user.setPassword("1111");
user.setNickName("update nickName");
// delete
em.remove(user);
요구사항 변경 전 요구사항 변경 후
모든 Domain은 변경된다.
● type safe 하지 않고
● 실수할 확률이 높고
● 수정해야 할 코드가 많아지며
● 단순 CRUD 코드를 반복한다.
객체와 테이블
테이블 지향 엔티티
public class User {
private int userId;
private String name;
private String password;
private String nickName;
}
public class Board {
private int boardId;
// foreign key
private int userId;
private String title;
private String content;
}
객체 지향 엔티티
public class User {
private int userId;
private String name;
private String password;
private String nickName;
}
public class Board {
private int boardId;
// object reference
private User user;
private String title;
private String content;
}
● 객체 그래프 탐색
● 테이블과 객체 간 연관 관계 불일치
● Query 작성 증가
엔티티 생명주기
● New(비영속) : DB에 반영 되지 않고 영속성 컨텍스트와 관
계 없는 엔티티
● Managed(영속) : 영속성 컨텍스트에 저장된 엔티티
● Detached(준영속) : 영속성 컨텍스트에서 분리된 엔티티
● Removed(삭제) : 삭제된 엔티티
비영속 그리고 준영속
// 비 영속
User newUser = new User(“name”, “password”, “nickName”);
// 준 영속
User detachUser = new User(1, “name”, “password”, “nickName”);
detachUser.userId가
DB에 있는 경우 “준영속”
영속성 컨텍스트
● 쓰기 지연 SQL
● 자동 변경 감지
● 1차 캐시
● 엔티티 동일성
● 지연 로딩
영속성 컨텍스트는 어떻게 동작할까?
@Entity
@Table(name = "User")
public class User {
@Id @GeneratedValue
private Integer userId;
@Column(name = "name", nullable = true)
private String name;
@Column(name = "password", nullable = true)
private String password;
@Column(name = "nickName", nullable = true)
private String nickName;
}
입력(persist)
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
// 비 영속 상태
User user = new User("wons", "12345", "woniper");
// 영속 상태
// 1차 캐시 저장
em.persist(user);
// 준영속 상태
// SQL 저장소 쿼리 반영
em.getTransaction().commit();
em.close();
조회(find)
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
// 영속 엔티티
// 1차 캐시 저장
User user = em.find(User.class, user.getUserId());
// 준영속 상태
em.getTransaction().commit();
em.close();
수정(자동 변경 감지)
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
// 영속 상태
// 1차 캐시 저장
User user = em.find(User.class, user.getUserId());
// 자동 변경 감지
user.setName("updateName");
user.setPassword("1111");
user.setNickName("updateNick");
// 준영속 상태
// SQL 저장소 쿼리 반영
em.getTransaction().commit();
em.close();
삭제(remove)
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
// 영속 상태, 1차 캐시 저장
User user = em.find(User.class, user.getUserId());
// 삭제 상태
em.remove(user);
// SQL 저장소 쿼리 반영
em.getTransaction().commit();
em.close();
지금까지 모든 일은 트랜젝션
작업 단위에서 동작
사실 commit은
EntityManager.flush()를 먼저 호출
flush()는 영속성 컨텍스트와 DB를 동기화
merge
준 영속 엔티티 -> 영속 엔티티
merge를 알아보기 전에
영속 엔티티 -> 준 영속 엔티티
● em.clear();
● em.detach(user);
● em.getTransaction().commit();
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
// 1. 영속 상태
User user1 = em.find(User.class, 1);
// 2. 준영속 상태
em.detach(user1);
// 3. name 속성 변경
user1.setName("lee-kyung-won");
// 4. 영속 상태
em.merge(user1);
em.getTransaction().commit();
em.close();
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
// 5. name 속성 값은?
User user2 = em.find(User.class, 1);
em.getTransaction().commit();
em.close();
● 속성이 변경된 준영속 엔티티 merge : update
● 비영속 엔티티 merge : insert
즉 merge는 영속 엔티티로 만들기도 하지
만
update 또는 insert 하기도 함
준 영속 상태는 영속성 컨텍스트
특징을 사용하지 못하는 것
● 쓰기 지연 SQL
● 자동 변경 감지
● 1차 캐시
● 엔티티 동일성
● 지연 로딩
Spring Data JPA
소개 정도만 할게요.
@Entity
@Table(name = "User")
public class User {
@Id @GeneratedValue
private Integer userId;
@Column(name = "name", nullable = true)
private String name;
@Column(name = "password", nullable = true)
private String password;
@Column(name = "nickName", nullable = true)
private String nickName;
}
Repository Interface
public interface UserRepository extends JpaRepository<User, Integer> {
}
Repository 만 추가 하면
● save(T);
● delete(T);
● findOne(ID);
● findAll();
● findAll(Pageable);
CRUD 그 외 조회 Query가
필요하다면?
Query Method
http://goo.gl/luWvvP
method 이름으로 Query생성
정확히 말하면 JPQL 생성
물론 규칙을 지켜 method 이름 설정
public interface UserRepository extends JpaRepository<User, Integer> {
// where u.name = ?name
User findByName(String name);
// where u.name = ?name and u.password = ?password;
User findByNameAndPassword(String name, String password);
}
참고자료
● 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
QnA
● blog : http://blog.woniper.net
● github : https://github.com/woniper
● email : leekw3747@gmail.com

More Related Content

What's hot

What's hot (20)

webservice scaling for newbie
webservice scaling for newbiewebservice scaling for newbie
webservice scaling for newbie
 
오딘: 발할라 라이징 MMORPG의 성능 최적화 사례 공유 [카카오게임즈 - 레벨 300] - 발표자: 김문권, 팀장, 라이온하트 스튜디오...
오딘: 발할라 라이징 MMORPG의 성능 최적화 사례 공유 [카카오게임즈 - 레벨 300] - 발표자: 김문권, 팀장, 라이온하트 스튜디오...오딘: 발할라 라이징 MMORPG의 성능 최적화 사례 공유 [카카오게임즈 - 레벨 300] - 발표자: 김문권, 팀장, 라이온하트 스튜디오...
오딘: 발할라 라이징 MMORPG의 성능 최적화 사례 공유 [카카오게임즈 - 레벨 300] - 발표자: 김문권, 팀장, 라이온하트 스튜디오...
 
Introduction to JPA and Hibernate including examples
Introduction to JPA and Hibernate including examplesIntroduction to JPA and Hibernate including examples
Introduction to JPA and Hibernate including examples
 
서버 성능에 대한 정의와 이해
서버 성능에 대한 정의와 이해서버 성능에 대한 정의와 이해
서버 성능에 대한 정의와 이해
 
영속성 컨텍스트로 보는 JPA
영속성 컨텍스트로 보는 JPA영속성 컨텍스트로 보는 JPA
영속성 컨텍스트로 보는 JPA
 
MMOG Server-Side 충돌 및 이동처리 설계와 구현
MMOG Server-Side 충돌 및 이동처리 설계와 구현MMOG Server-Side 충돌 및 이동처리 설계와 구현
MMOG Server-Side 충돌 및 이동처리 설계와 구현
 
Iocp 기본 구조 이해
Iocp 기본 구조 이해Iocp 기본 구조 이해
Iocp 기본 구조 이해
 
[NDC2016] TERA 서버의 Modern C++ 활용기
[NDC2016] TERA 서버의 Modern C++ 활용기[NDC2016] TERA 서버의 Modern C++ 활용기
[NDC2016] TERA 서버의 Modern C++ 활용기
 
[C++ Korea] C++ 메모리 모델과 atomic 타입 연산들
[C++ Korea] C++ 메모리 모델과 atomic 타입 연산들[C++ Korea] C++ 메모리 모델과 atomic 타입 연산들
[C++ Korea] C++ 메모리 모델과 atomic 타입 연산들
 
Presto, Zeppelin을 이용한 초간단 BI 구축 사례
Presto, Zeppelin을 이용한 초간단 BI 구축 사례Presto, Zeppelin을 이용한 초간단 BI 구축 사례
Presto, Zeppelin을 이용한 초간단 BI 구축 사례
 
Java ORマッパー選定のポイント #jsug
Java ORマッパー選定のポイント #jsugJava ORマッパー選定のポイント #jsug
Java ORマッパー選定のポイント #jsug
 
Windows Registered I/O (RIO) vs IOCP
Windows Registered I/O (RIO) vs IOCPWindows Registered I/O (RIO) vs IOCP
Windows Registered I/O (RIO) vs IOCP
 
DDD로 복잡함 다루기
DDD로 복잡함 다루기DDD로 복잡함 다루기
DDD로 복잡함 다루기
 
Where狙いのキー、order by狙いのキー
Where狙いのキー、order by狙いのキーWhere狙いのキー、order by狙いのキー
Where狙いのキー、order by狙いのキー
 
중앙 서버 없는 게임 로직
중앙 서버 없는 게임 로직중앙 서버 없는 게임 로직
중앙 서버 없는 게임 로직
 
SQLアンチパターン - 開発者を待ち受ける25の落とし穴 (拡大版)
SQLアンチパターン - 開発者を待ち受ける25の落とし穴 (拡大版)SQLアンチパターン - 開発者を待ち受ける25の落とし穴 (拡大版)
SQLアンチパターン - 開発者を待ち受ける25の落とし穴 (拡大版)
 
NoSQL 위에서 MMORPG 개발하기
NoSQL 위에서 MMORPG 개발하기NoSQL 위에서 MMORPG 개발하기
NoSQL 위에서 MMORPG 개발하기
 
[125]웹 성능 최적화에 필요한 브라우저의 모든 것
[125]웹 성능 최적화에 필요한 브라우저의 모든 것[125]웹 성능 최적화에 필요한 브라우저의 모든 것
[125]웹 성능 최적화에 필요한 브라우저의 모든 것
 
Ksug2015 - JPA1, JPA 소개
Ksug2015 - JPA1, JPA 소개Ksug2015 - JPA1, JPA 소개
Ksug2015 - JPA1, JPA 소개
 
로그 기깔나게 잘 디자인하는 법
로그 기깔나게 잘 디자인하는 법로그 기깔나게 잘 디자인하는 법
로그 기깔나게 잘 디자인하는 법
 

Similar to Jpa 잘 (하는 척) 하기

vine webdev
vine webdevvine webdev
vine webdev
dcfc1997
 

Similar to Jpa 잘 (하는 척) 하기 (20)

Jlook open api platform-appdevguide
Jlook open api platform-appdevguideJlook open api platform-appdevguide
Jlook open api platform-appdevguide
 
[2018] MyBatis에서 JPA로
[2018] MyBatis에서 JPA로[2018] MyBatis에서 JPA로
[2018] MyBatis에서 JPA로
 
MyBatis에서 JPA로
MyBatis에서 JPA로MyBatis에서 JPA로
MyBatis에서 JPA로
 
Spring Boot 2
Spring Boot 2Spring Boot 2
Spring Boot 2
 
Ksug2015 - JPA3, JPA 내부구조
Ksug2015 - JPA3, JPA 내부구조Ksug2015 - JPA3, JPA 내부구조
Ksug2015 - JPA3, JPA 내부구조
 
[Study]HeadFirst JSP&servlet chapter5
[Study]HeadFirst JSP&servlet chapter5[Study]HeadFirst JSP&servlet chapter5
[Study]HeadFirst JSP&servlet chapter5
 
#16.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis)_국비지원IT학원/실업자/재직자환급교육/자바/스프링/...
#16.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis)_국비지원IT학원/실업자/재직자환급교육/자바/스프링/...#16.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis)_국비지원IT학원/실업자/재직자환급교육/자바/스프링/...
#16.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis)_국비지원IT학원/실업자/재직자환급교육/자바/스프링/...
 
스프링처럼 JDBC 리팩터링하기
스프링처럼 JDBC 리팩터링하기 스프링처럼 JDBC 리팩터링하기
스프링처럼 JDBC 리팩터링하기
 
Java JPA
Java JPAJava JPA
Java JPA
 
[스프링 스터디 2일차] 서비스 추상화
[스프링 스터디 2일차] 서비스 추상화[스프링 스터디 2일차] 서비스 추상화
[스프링 스터디 2일차] 서비스 추상화
 
JPA Study - 1주차(SLIPP)
JPA Study - 1주차(SLIPP)JPA Study - 1주차(SLIPP)
JPA Study - 1주차(SLIPP)
 
자바 웹 개발 시작하기 (6주차 : 커뮤니티를 만들어보자!)
자바 웹 개발 시작하기 (6주차 : 커뮤니티를 만들어보자!)자바 웹 개발 시작하기 (6주차 : 커뮤니티를 만들어보자!)
자바 웹 개발 시작하기 (6주차 : 커뮤니티를 만들어보자!)
 
overview of spring4
overview of spring4overview of spring4
overview of spring4
 
MyBatis 개요와 Java+MyBatis+MySQL 예제
MyBatis 개요와 Java+MyBatis+MySQL 예제MyBatis 개요와 Java+MyBatis+MySQL 예제
MyBatis 개요와 Java+MyBatis+MySQL 예제
 
Node.js and react
Node.js and reactNode.js and react
Node.js and react
 
vine webdev
vine webdevvine webdev
vine webdev
 
Daejeon IT Developer Conference Hibernate3
Daejeon IT Developer Conference Hibernate3Daejeon IT Developer Conference Hibernate3
Daejeon IT Developer Conference Hibernate3
 
Xe hack
Xe hackXe hack
Xe hack
 
Ksug2015 jpa4 객체지향쿼리
Ksug2015 jpa4 객체지향쿼리Ksug2015 jpa4 객체지향쿼리
Ksug2015 jpa4 객체지향쿼리
 
Swt J Face 2/3
Swt J Face 2/3Swt J Face 2/3
Swt J Face 2/3
 

Recently uploaded

Grid Layout (Kitworks Team Study 장현정 발표자료)
Grid Layout (Kitworks Team Study 장현정 발표자료)Grid Layout (Kitworks Team Study 장현정 발표자료)
Grid Layout (Kitworks Team Study 장현정 발표자료)
Wonjun Hwang
 
파일 업로드(Kitworks Team Study 유현주 발표자료 240510)
파일 업로드(Kitworks Team Study 유현주 발표자료 240510)파일 업로드(Kitworks Team Study 유현주 발표자료 240510)
파일 업로드(Kitworks Team Study 유현주 발표자료 240510)
Wonjun Hwang
 

Recently uploaded (6)

클라우드 애플리케이션 보안 플랫폼 'Checkmarx One' 소개자료
클라우드 애플리케이션 보안 플랫폼 'Checkmarx One' 소개자료클라우드 애플리케이션 보안 플랫폼 'Checkmarx One' 소개자료
클라우드 애플리케이션 보안 플랫폼 'Checkmarx One' 소개자료
 
도심 하늘에서 시속 200km로 비행할 수 있는 미래 항공 모빌리티 'S-A2'
도심 하늘에서 시속 200km로 비행할 수 있는 미래 항공 모빌리티 'S-A2'도심 하늘에서 시속 200km로 비행할 수 있는 미래 항공 모빌리티 'S-A2'
도심 하늘에서 시속 200km로 비행할 수 있는 미래 항공 모빌리티 'S-A2'
 
Grid Layout (Kitworks Team Study 장현정 발표자료)
Grid Layout (Kitworks Team Study 장현정 발표자료)Grid Layout (Kitworks Team Study 장현정 발표자료)
Grid Layout (Kitworks Team Study 장현정 발표자료)
 
파일 업로드(Kitworks Team Study 유현주 발표자료 240510)
파일 업로드(Kitworks Team Study 유현주 발표자료 240510)파일 업로드(Kitworks Team Study 유현주 발표자료 240510)
파일 업로드(Kitworks Team Study 유현주 발표자료 240510)
 
오픈소스 위험 관리 및 공급망 보안 솔루션 'Checkmarx SCA' 소개자료
오픈소스 위험 관리 및 공급망 보안 솔루션 'Checkmarx SCA' 소개자료오픈소스 위험 관리 및 공급망 보안 솔루션 'Checkmarx SCA' 소개자료
오픈소스 위험 관리 및 공급망 보안 솔루션 'Checkmarx SCA' 소개자료
 
[OpenLAB] AWS reInvent를 통해 바라본 글로벌 Cloud 기술동향.pdf
[OpenLAB] AWS reInvent를 통해 바라본 글로벌 Cloud 기술동향.pdf[OpenLAB] AWS reInvent를 통해 바라본 글로벌 Cloud 기술동향.pdf
[OpenLAB] AWS reInvent를 통해 바라본 글로벌 Cloud 기술동향.pdf
 

Jpa 잘 (하는 척) 하기

  • 1. JPA 잘 (하는 척) 하기 SLiPP-JPA 이경원
  • 2. 목차 ● JPA 소개 ● JDBC부터 JPA까지 ● 객체와 테이블 ● 엔티티 생명주기 ● 영속성 컨텍스트 ● Spring Data JPA ● 참고 자료 ● QnA
  • 4. ● Java ORM(Object-Relational Mapping) 표준 F/W ● RDB를 객체로 표현(매핑), 사용 ● Hibernate, Eclipse Link, TopLink Essensials 구현체
  • 7. 단순 CRU(입력, 조회, 수정) public class User { private int userId; private String name; private String password; }
  • 9. // connection… 1. 쿼리 생성 String insertQuery = "insert into User(userId, name, password) values(?, ?, ?)"; 2. parameter 매핑 pstmt.setInt(1, user.getUserId()); pstmt.setString(2, user.getName()); pstmt.setString(3, user.getPassword()); 3. 쿼리 실행 pstmt.executeUpdate(insertQuery); // close...
  • 10. // connection… 1. 쿼리 생성 String selectQuery = "select userId, name, password from User where userId =" + user.getUserId(); 2. 쿼리 실행 ResultSet rs = stmt.executeQuery(selectQuery); 3. 데이터 매핑 user.setUserId(rs.getInt("userId")); user.setName(rs.getString("name")); user.setPassword(rs.getString("password")); // close...
  • 11. // connection… 1. 쿼리 생성 String updateQuery = "update User set name = ?, password = ? where userId = ?"; 2. parameter 매핑 pstmt.setString(1, user.getName()); pstmt.setString(2, user.getPassword()); pstmt.setInt(3, user.getUserId()); 3. 쿼리 실행 pstmt.executeUpdate(updateQuery); // close...
  • 12. Query 생성, 실행, 데이터 매핑 반복
  • 15. 1. insert <insert id="inser" parameterType="User"> insert into User(userId, name, password) values (#{userId}, #{name}, #{password}) </insert> 2. select <select id="select" parameterType="java.lang.Integer" resultType="User"> select userId, name, password from User where userId = #{userId} </select> 3. update <update id="update" parameterType="User"> update User set name = #{name}, password = #{password} where userId = #{userId} </update>
  • 16. Connection, close 위임 parameter 매핑, 데이터 매핑 위임
  • 17. 하지만 여전히 Query 작성 반복
  • 18. JPA
  • 19. EntityManager em = entityManagerFactory.createEntityManager(); // Insert em.persist(user); // Select User user = em.find(User.class, user.getUserId()); // Update user.setName("update Name"); user.setPassword("1111"); // Delete em.remove(user);
  • 20. Connection, close 위임 쿼리 자동 생성, 실행
  • 22. public class User { private int userId; private String name; private String password; // nickName 컬럼 추가 private String nickName; }
  • 23. JDBC
  • 24. 1. insert 쿼리 생성 String insertQuery = "insert into User(userId, name, password, nickName) values(?, ?, ?, ?)"; 2. parameter 매핑 pstmt.setInt(1, user.getUserId()); pstmt.setString(2, user.getName()); pstmt.setString(3, user.getPassword()); pstmt.setString(4, user.getNickName()); 3. 쿼리 실행 pstmt.executeUpdate(insertQuery);
  • 25. 1. select 쿼리 생성 String selectQuery = "select userId, name, password, nickname from User where userId = " + user.getUserId(); 2. 쿼리 실행 ResultSet rs = stmt.executeQuery(selectQuery); 3. 데이터 매핑 user.setUserId(rs.getInt("userId")); user.setName(rs.getString("name")); user.setPassword(rs.getString("password")); user.setNickName(rs.getString("nickName"));
  • 26. 1. update 쿼리 생성 String updateQuery = "update User set name = ?, password = ?, nickName = ? where userId = ?"; 2. parameter 매핑 pstmt.setString(1, user.getName()); pstmt.setString(2, user.getPassword()); pstmt.setString(3, user.getNickName()); pstmt.setInt(4, user.getUserId()); 3. 쿼리 실행 pstmt.executeUpdate(updateQuery);
  • 27. Query 수정, 데이터 매핑 코드 추가
  • 29. 1. insert <insert id="inser" parameterType="User"> insert into User(userId, name, password, nickName) values (#{userId}, #{name}, #{password}, #{nickName}) </insert> 2. select <select id="select" parameterType="java.lang.Integer" resultType="User"> select userId, name, password, nickName from User where userId = #{userId} </select> 3. update <update id="update" parameterType="User"> update User set name = #{name}, password = #{password}, nickName = #{nickName} where userId = #{userId} </update>
  • 32. // insert em.persist(user); // select em.find(User.class, user.getUserId()); // update user.setName("update Name"); user.setPassword("1111"); // delete em.remove(user); // insert em.persist(user); // select em.find(User.class, user.getUserId()); // update user.setName("update Name"); user.setPassword("1111"); user.setNickName("update nickName"); // delete em.remove(user); 요구사항 변경 전 요구사항 변경 후
  • 34. ● type safe 하지 않고 ● 실수할 확률이 높고 ● 수정해야 할 코드가 많아지며 ● 단순 CRUD 코드를 반복한다.
  • 36. 테이블 지향 엔티티 public class User { private int userId; private String name; private String password; private String nickName; } public class Board { private int boardId; // foreign key private int userId; private String title; private String content; }
  • 37. 객체 지향 엔티티 public class User { private int userId; private String name; private String password; private String nickName; } public class Board { private int boardId; // object reference private User user; private String title; private String content; }
  • 38. ● 객체 그래프 탐색 ● 테이블과 객체 간 연관 관계 불일치 ● Query 작성 증가
  • 40. ● New(비영속) : DB에 반영 되지 않고 영속성 컨텍스트와 관 계 없는 엔티티 ● Managed(영속) : 영속성 컨텍스트에 저장된 엔티티 ● Detached(준영속) : 영속성 컨텍스트에서 분리된 엔티티 ● Removed(삭제) : 삭제된 엔티티
  • 41.
  • 43. // 비 영속 User newUser = new User(“name”, “password”, “nickName”); // 준 영속 User detachUser = new User(1, “name”, “password”, “nickName”);
  • 46. ● 쓰기 지연 SQL ● 자동 변경 감지 ● 1차 캐시 ● 엔티티 동일성 ● 지연 로딩
  • 48. @Entity @Table(name = "User") public class User { @Id @GeneratedValue private Integer userId; @Column(name = "name", nullable = true) private String name; @Column(name = "password", nullable = true) private String password; @Column(name = "nickName", nullable = true) private String nickName; }
  • 50. EntityManager em = emf.createEntityManager(); em.getTransaction().begin(); // 비 영속 상태 User user = new User("wons", "12345", "woniper"); // 영속 상태 // 1차 캐시 저장 em.persist(user); // 준영속 상태 // SQL 저장소 쿼리 반영 em.getTransaction().commit(); em.close();
  • 51.
  • 52.
  • 53.
  • 55. EntityManager em = emf.createEntityManager(); em.getTransaction().begin(); // 영속 엔티티 // 1차 캐시 저장 User user = em.find(User.class, user.getUserId()); // 준영속 상태 em.getTransaction().commit(); em.close();
  • 56.
  • 57.
  • 58.
  • 60. EntityManager em = emf.createEntityManager(); em.getTransaction().begin(); // 영속 상태 // 1차 캐시 저장 User user = em.find(User.class, user.getUserId()); // 자동 변경 감지 user.setName("updateName"); user.setPassword("1111"); user.setNickName("updateNick"); // 준영속 상태 // SQL 저장소 쿼리 반영 em.getTransaction().commit(); em.close();
  • 61.
  • 62.
  • 63.
  • 65. EntityManager em = emf.createEntityManager(); em.getTransaction().begin(); // 영속 상태, 1차 캐시 저장 User user = em.find(User.class, user.getUserId()); // 삭제 상태 em.remove(user); // SQL 저장소 쿼리 반영 em.getTransaction().commit(); em.close();
  • 66.
  • 67.
  • 68.
  • 69. 지금까지 모든 일은 트랜젝션 작업 단위에서 동작
  • 72. merge
  • 73. 준 영속 엔티티 -> 영속 엔티티
  • 75. 영속 엔티티 -> 준 영속 엔티티
  • 76. ● em.clear(); ● em.detach(user); ● em.getTransaction().commit();
  • 77. EntityManager em = emf.createEntityManager(); em.getTransaction().begin(); // 1. 영속 상태 User user1 = em.find(User.class, 1); // 2. 준영속 상태 em.detach(user1); // 3. name 속성 변경 user1.setName("lee-kyung-won"); // 4. 영속 상태 em.merge(user1); em.getTransaction().commit(); em.close();
  • 78. EntityManager em = emf.createEntityManager(); em.getTransaction().begin(); // 5. name 속성 값은? User user2 = em.find(User.class, 1); em.getTransaction().commit(); em.close();
  • 79. ● 속성이 변경된 준영속 엔티티 merge : update ● 비영속 엔티티 merge : insert
  • 80. 즉 merge는 영속 엔티티로 만들기도 하지 만 update 또는 insert 하기도 함
  • 81. 준 영속 상태는 영속성 컨텍스트 특징을 사용하지 못하는 것
  • 82. ● 쓰기 지연 SQL ● 자동 변경 감지 ● 1차 캐시 ● 엔티티 동일성 ● 지연 로딩
  • 85. @Entity @Table(name = "User") public class User { @Id @GeneratedValue private Integer userId; @Column(name = "name", nullable = true) private String name; @Column(name = "password", nullable = true) private String password; @Column(name = "nickName", nullable = true) private String nickName; }
  • 87. public interface UserRepository extends JpaRepository<User, Integer> { }
  • 89. ● save(T); ● delete(T); ● findOne(ID); ● findAll(); ● findAll(Pageable);
  • 90. CRUD 그 외 조회 Query가 필요하다면?
  • 94. 물론 규칙을 지켜 method 이름 설정
  • 95. public interface UserRepository extends JpaRepository<User, Integer> { // where u.name = ?name User findByName(String name); // where u.name = ?name and u.password = ?password; User findByNameAndPassword(String name, String password); }
  • 97.
  • 98. ● http://www.tutorialspoint.com/jpa/ ● http://www.objectdb.com/ ● https://en.wikibooks. org/wiki/Java_Persistence
  • 99. ● http://goo.gl/xzpTdK ● https://goo.gl/sqmO9p ● https://goo.gl/GhsI4Q ● https://goo.gl/GpQzeL
  • 100. QnA
  • 101. ● blog : http://blog.woniper.net ● github : https://github.com/woniper ● email : leekw3747@gmail.com