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

Workshop spring session 2 - La persistance au sein des applications Java
Workshop spring   session 2 - La persistance au sein des applications JavaWorkshop spring   session 2 - La persistance au sein des applications Java
Workshop spring session 2 - La persistance au sein des applications JavaAntoine Rey
 
우아한 객체지향
우아한 객체지향우아한 객체지향
우아한 객체지향Young-Ho Cho
 
자바에서 null을 안전하게 다루는 방법
자바에서 null을 안전하게 다루는 방법자바에서 null을 안전하게 다루는 방법
자바에서 null을 안전하게 다루는 방법Sungchul Park
 
Ksug2015 - JPA1, JPA 소개
Ksug2015 - JPA1, JPA 소개Ksug2015 - JPA1, JPA 소개
Ksug2015 - JPA1, JPA 소개Younghan Kim
 
Advanced Javascript
Advanced JavascriptAdvanced Javascript
Advanced JavascriptAdieu
 
[오픈소스컨설팅] 스카우터 사용자 가이드 2020
[오픈소스컨설팅] 스카우터 사용자 가이드 2020[오픈소스컨설팅] 스카우터 사용자 가이드 2020
[오픈소스컨설팅] 스카우터 사용자 가이드 2020Ji-Woong Choi
 
도메인 주도 설계의 본질
도메인 주도 설계의 본질도메인 주도 설계의 본질
도메인 주도 설계의 본질Young-Ho Cho
 
애플리케이션 아키텍처와 객체지향
애플리케이션 아키텍처와 객체지향 애플리케이션 아키텍처와 객체지향
애플리케이션 아키텍처와 객체지향 Young-Ho Cho
 
스프링 시큐리티 구조 이해
스프링 시큐리티 구조 이해스프링 시큐리티 구조 이해
스프링 시큐리티 구조 이해beom kyun choi
 
Idiomatic Kotlin
Idiomatic KotlinIdiomatic Kotlin
Idiomatic Kotlinintelliyole
 
Naver속도의, 속도에 의한, 속도를 위한 몽고DB (네이버 컨텐츠검색과 몽고DB) [Naver]
Naver속도의, 속도에 의한, 속도를 위한 몽고DB (네이버 컨텐츠검색과 몽고DB) [Naver]Naver속도의, 속도에 의한, 속도를 위한 몽고DB (네이버 컨텐츠검색과 몽고DB) [Naver]
Naver속도의, 속도에 의한, 속도를 위한 몽고DB (네이버 컨텐츠검색과 몽고DB) [Naver]MongoDB
 
[2D1]Elasticsearch 성능 최적화
[2D1]Elasticsearch 성능 최적화[2D1]Elasticsearch 성능 최적화
[2D1]Elasticsearch 성능 최적화NAVER D2
 
쿠키런 1년, 서버개발 분투기
쿠키런 1년, 서버개발 분투기쿠키런 1년, 서버개발 분투기
쿠키런 1년, 서버개발 분투기Brian Hong
 
Angular Directives | Angular 2 Custom Directives | Angular Tutorial | Angular...
Angular Directives | Angular 2 Custom Directives | Angular Tutorial | Angular...Angular Directives | Angular 2 Custom Directives | Angular Tutorial | Angular...
Angular Directives | Angular 2 Custom Directives | Angular Tutorial | Angular...Edureka!
 
[오픈소스컨설팅]Scouter 설치 및 사용가이드(JBoss)
[오픈소스컨설팅]Scouter 설치 및 사용가이드(JBoss)[오픈소스컨설팅]Scouter 설치 및 사용가이드(JBoss)
[오픈소스컨설팅]Scouter 설치 및 사용가이드(JBoss)Ji-Woong Choi
 
Lecture 10 - Java Server Faces (JSF)
Lecture 10 - Java Server Faces (JSF)Lecture 10 - Java Server Faces (JSF)
Lecture 10 - Java Server Faces (JSF)Fahad Golra
 

What's hot (20)

Workshop spring session 2 - La persistance au sein des applications Java
Workshop spring   session 2 - La persistance au sein des applications JavaWorkshop spring   session 2 - La persistance au sein des applications Java
Workshop spring session 2 - La persistance au sein des applications Java
 
Rich domain model
Rich domain modelRich domain model
Rich domain model
 
우아한 객체지향
우아한 객체지향우아한 객체지향
우아한 객체지향
 
자바에서 null을 안전하게 다루는 방법
자바에서 null을 안전하게 다루는 방법자바에서 null을 안전하게 다루는 방법
자바에서 null을 안전하게 다루는 방법
 
Ksug2015 - JPA1, JPA 소개
Ksug2015 - JPA1, JPA 소개Ksug2015 - JPA1, JPA 소개
Ksug2015 - JPA1, JPA 소개
 
Advanced Javascript
Advanced JavascriptAdvanced Javascript
Advanced Javascript
 
[오픈소스컨설팅] 스카우터 사용자 가이드 2020
[오픈소스컨설팅] 스카우터 사용자 가이드 2020[오픈소스컨설팅] 스카우터 사용자 가이드 2020
[오픈소스컨설팅] 스카우터 사용자 가이드 2020
 
도메인 주도 설계의 본질
도메인 주도 설계의 본질도메인 주도 설계의 본질
도메인 주도 설계의 본질
 
NodeJS for Beginner
NodeJS for BeginnerNodeJS for Beginner
NodeJS for Beginner
 
Spring Data JPA
Spring Data JPASpring Data JPA
Spring Data JPA
 
Spring Boot
Spring BootSpring Boot
Spring Boot
 
애플리케이션 아키텍처와 객체지향
애플리케이션 아키텍처와 객체지향 애플리케이션 아키텍처와 객체지향
애플리케이션 아키텍처와 객체지향
 
스프링 시큐리티 구조 이해
스프링 시큐리티 구조 이해스프링 시큐리티 구조 이해
스프링 시큐리티 구조 이해
 
Idiomatic Kotlin
Idiomatic KotlinIdiomatic Kotlin
Idiomatic Kotlin
 
Naver속도의, 속도에 의한, 속도를 위한 몽고DB (네이버 컨텐츠검색과 몽고DB) [Naver]
Naver속도의, 속도에 의한, 속도를 위한 몽고DB (네이버 컨텐츠검색과 몽고DB) [Naver]Naver속도의, 속도에 의한, 속도를 위한 몽고DB (네이버 컨텐츠검색과 몽고DB) [Naver]
Naver속도의, 속도에 의한, 속도를 위한 몽고DB (네이버 컨텐츠검색과 몽고DB) [Naver]
 
[2D1]Elasticsearch 성능 최적화
[2D1]Elasticsearch 성능 최적화[2D1]Elasticsearch 성능 최적화
[2D1]Elasticsearch 성능 최적화
 
쿠키런 1년, 서버개발 분투기
쿠키런 1년, 서버개발 분투기쿠키런 1년, 서버개발 분투기
쿠키런 1년, 서버개발 분투기
 
Angular Directives | Angular 2 Custom Directives | Angular Tutorial | Angular...
Angular Directives | Angular 2 Custom Directives | Angular Tutorial | Angular...Angular Directives | Angular 2 Custom Directives | Angular Tutorial | Angular...
Angular Directives | Angular 2 Custom Directives | Angular Tutorial | Angular...
 
[오픈소스컨설팅]Scouter 설치 및 사용가이드(JBoss)
[오픈소스컨설팅]Scouter 설치 및 사용가이드(JBoss)[오픈소스컨설팅]Scouter 설치 및 사용가이드(JBoss)
[오픈소스컨설팅]Scouter 설치 및 사용가이드(JBoss)
 
Lecture 10 - Java Server Faces (JSF)
Lecture 10 - Java Server Faces (JSF)Lecture 10 - Java Server Faces (JSF)
Lecture 10 - Java Server Faces (JSF)
 

Similar to Jpa 잘 (하는 척) 하기

Jlook open api platform-appdevguide
Jlook open api platform-appdevguideJlook open api platform-appdevguide
Jlook open api platform-appdevguideHongSeong Jeon
 
[2018] MyBatis에서 JPA로
[2018] MyBatis에서 JPA로[2018] MyBatis에서 JPA로
[2018] MyBatis에서 JPA로NHN FORWARD
 
MyBatis에서 JPA로
MyBatis에서 JPA로MyBatis에서 JPA로
MyBatis에서 JPA로Dongmin Shin
 
[Study]HeadFirst JSP&servlet chapter5
[Study]HeadFirst JSP&servlet chapter5[Study]HeadFirst JSP&servlet chapter5
[Study]HeadFirst JSP&servlet chapter5Hyeonseok Yang
 
#16.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis)_국비지원IT학원/실업자/재직자환급교육/자바/스프링/...
#16.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis)_국비지원IT학원/실업자/재직자환급교육/자바/스프링/...#16.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis)_국비지원IT학원/실업자/재직자환급교육/자바/스프링/...
#16.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis)_국비지원IT학원/실업자/재직자환급교육/자바/스프링/...탑크리에듀(구로디지털단지역3번출구 2분거리)
 
스프링처럼 JDBC 리팩터링하기
스프링처럼 JDBC 리팩터링하기 스프링처럼 JDBC 리팩터링하기
스프링처럼 JDBC 리팩터링하기 Chanwook Park
 
[스프링 스터디 2일차] 서비스 추상화
[스프링 스터디 2일차] 서비스 추상화[스프링 스터디 2일차] 서비스 추상화
[스프링 스터디 2일차] 서비스 추상화AnselmKim
 
JPA Study - 1주차(SLIPP)
JPA Study - 1주차(SLIPP)JPA Study - 1주차(SLIPP)
JPA Study - 1주차(SLIPP)Jeong-gyu Kim
 
자바 웹 개발 시작하기 (6주차 : 커뮤니티를 만들어보자!)
자바 웹 개발 시작하기 (6주차 : 커뮤니티를 만들어보자!)자바 웹 개발 시작하기 (6주차 : 커뮤니티를 만들어보자!)
자바 웹 개발 시작하기 (6주차 : 커뮤니티를 만들어보자!)DK Lee
 
overview of spring4
overview of spring4overview of spring4
overview of spring4Arawn Park
 
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 reactHyungKuIm
 
vine webdev
vine webdevvine webdev
vine webdevdcfc1997
 
Daejeon IT Developer Conference Hibernate3
Daejeon IT Developer Conference Hibernate3Daejeon IT Developer Conference Hibernate3
Daejeon IT Developer Conference Hibernate3plusperson
 
Ksug2015 jpa4 객체지향쿼리
Ksug2015 jpa4 객체지향쿼리Ksug2015 jpa4 객체지향쿼리
Ksug2015 jpa4 객체지향쿼리Younghan Kim
 
[162] jpa와 모던 자바 데이터 저장 기술
[162] jpa와 모던 자바 데이터 저장 기술[162] jpa와 모던 자바 데이터 저장 기술
[162] jpa와 모던 자바 데이터 저장 기술NAVER D2
 

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
 
[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
 
[162] jpa와 모던 자바 데이터 저장 기술
[162] jpa와 모던 자바 데이터 저장 기술[162] jpa와 모던 자바 데이터 저장 기술
[162] jpa와 모던 자바 데이터 저장 기술
 

Recently uploaded

Console API (Kitworks Team Study 백혜인 발표자료)
Console API (Kitworks Team Study 백혜인 발표자료)Console API (Kitworks Team Study 백혜인 발표자료)
Console API (Kitworks Team Study 백혜인 발표자료)Wonjun Hwang
 
MOODv2 : Masked Image Modeling for Out-of-Distribution Detection
MOODv2 : Masked Image Modeling for Out-of-Distribution DetectionMOODv2 : Masked Image Modeling for Out-of-Distribution Detection
MOODv2 : Masked Image Modeling for Out-of-Distribution DetectionKim Daeun
 
Merge (Kitworks Team Study 이성수 발표자료 240426)
Merge (Kitworks Team Study 이성수 발표자료 240426)Merge (Kitworks Team Study 이성수 발표자료 240426)
Merge (Kitworks Team Study 이성수 발표자료 240426)Wonjun Hwang
 
A future that integrates LLMs and LAMs (Symposium)
A future that integrates LLMs and LAMs (Symposium)A future that integrates LLMs and LAMs (Symposium)
A future that integrates LLMs and LAMs (Symposium)Tae Young Lee
 
캐드앤그래픽스 2024년 5월호 목차
캐드앤그래픽스 2024년 5월호 목차캐드앤그래픽스 2024년 5월호 목차
캐드앤그래픽스 2024년 5월호 목차캐드앤그래픽스
 
Continual Active Learning for Efficient Adaptation of Machine LearningModels ...
Continual Active Learning for Efficient Adaptation of Machine LearningModels ...Continual Active Learning for Efficient Adaptation of Machine LearningModels ...
Continual Active Learning for Efficient Adaptation of Machine LearningModels ...Kim Daeun
 

Recently uploaded (6)

Console API (Kitworks Team Study 백혜인 발표자료)
Console API (Kitworks Team Study 백혜인 발표자료)Console API (Kitworks Team Study 백혜인 발표자료)
Console API (Kitworks Team Study 백혜인 발표자료)
 
MOODv2 : Masked Image Modeling for Out-of-Distribution Detection
MOODv2 : Masked Image Modeling for Out-of-Distribution DetectionMOODv2 : Masked Image Modeling for Out-of-Distribution Detection
MOODv2 : Masked Image Modeling for Out-of-Distribution Detection
 
Merge (Kitworks Team Study 이성수 발표자료 240426)
Merge (Kitworks Team Study 이성수 발표자료 240426)Merge (Kitworks Team Study 이성수 발표자료 240426)
Merge (Kitworks Team Study 이성수 발표자료 240426)
 
A future that integrates LLMs and LAMs (Symposium)
A future that integrates LLMs and LAMs (Symposium)A future that integrates LLMs and LAMs (Symposium)
A future that integrates LLMs and LAMs (Symposium)
 
캐드앤그래픽스 2024년 5월호 목차
캐드앤그래픽스 2024년 5월호 목차캐드앤그래픽스 2024년 5월호 목차
캐드앤그래픽스 2024년 5월호 목차
 
Continual Active Learning for Efficient Adaptation of Machine LearningModels ...
Continual Active Learning for Efficient Adaptation of Machine LearningModels ...Continual Active Learning for Efficient Adaptation of Machine LearningModels ...
Continual Active Learning for Efficient Adaptation of Machine LearningModels ...
 

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