Skip to content

Latest commit

ย 

History

History
359 lines (224 loc) ยท 12.4 KB

Study_JPA_01.md

File metadata and controls

359 lines (224 loc) ยท 12.4 KB

[์Šคํ„ฐ๋””] ์ž๋ฐ” ORM ํ‘œ์ค€ JPA ํ”„๋กœ๊ทธ๋ž˜๋ฐ 01

SQL vs JPA ์š”์•ฝ

๋น„๊ต

  • CRUD ์šฉ SQL ์ž‘์—… โ†’ ๋ฐ˜๋ณต์ž‘์—…, ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง ์ž‘์—… < SQL ์ž‘์„ฑ ์‹œ๊ฐ„
  • ์ง€๋ฃจํ•˜๊ณ  ๋น„์ƒ์‚ฐ์ ์ž„
  • ๊ฐ์ฒด ๋ชจ๋ธ๋ง๊ณผ ๊ด€๊ณ„ํ˜• ๋ฐ์ดํ„ฐ ๋ฒ ์ด์Šค ์‚ฌ์ด์˜ ์ฐจ์ด์  ํ•ด๊ฒฐ
  • ์‹คํ–‰ ์‹œ์ ์— ์ž๋™์œผ๋กœ SQL ๋งŒ๋“ค์–ด์„œ ์‹คํ–‰ โ†’ SQL์„ ์ง์ ‘ ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ์–ด๋–ค SQL ์‹คํ–‰

JPA ์žฅ์ 

  1. ๊ฐ์ฒด ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ โ†’ ์ƒ์‚ฐ์„ฑ, ์œ ์ง€๋ณด์ˆ˜ ๐Ÿ‘
  2. ํ…Œ์Šคํฌ ์šฉ์ด

SQL์„ ์ง์ ‘ ๋‹ค๋ฃฐ ๋•Œ์˜ ๋ฌธ์ œ์ 

์š”๊ตฌ์‚ฌํ•ญ : ํšŒ์› ๊ด€๋ฆฌ ๊ธฐ๋Šฅ โ†’ CRUD ๊ธฐ๋Šฅ ๊ฐœ๋ฐœ

1. ๋ฐ˜๋ณต

์˜ˆ์ œ) ํšŒ์› ์กฐํšŒ ๊ธฐ๋Šฅ์„ ๋งŒ๋“œ๋ ค๋ฉด?

ํšŒ์› ๊ฐ์ฒด(Member)์™€ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์ ‘๊ทผํ•˜๋Š” DAO ๊ฐ์ฒด

public class Member {
  
  private String memberId;
  private String name;
  // ..
  
}
public class MemberDAO {
  
  public Member find(String memberId)
			{ ... }
}

1. ํšŒ์› ์กฐํšŒ์šฉ SQL ์ž‘์„ฑ select member_id, name from member where member_id = ? 2. JDBC API๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ SQL ์‹คํ–‰ ResultSet rs = stmt.excuteQuery(sql); 3. ์ฟผ๋ฆฌ ์กฐํ™” ๊ฒฐ๊ณผ์™€ Member ๊ฐ์ฒด ๋งคํ•‘ String memberId = rs.getString("member_id"); String name = rs.getString("name");

Member member = new Member(); member.setMemberId(memberId); member.setName(name); // ...

๋ฌธ์ œ์ 

๊ฐ์ฒด๋ฅผ DB์— CRUD๋ฅผ ํ•˜๋ ค๋ฉด ๋„ˆ๋ฌด ๋งŽ์€ SQL๊ณผ JDBC API๋ฅผ ์ฝ”๋“œ๋กœ ์ž‘์„ฑํ•ด์•ผํ•จ

2. SQL์— ์˜์กด์ ์ธ ๊ฐœ๋ฐœ

์š”๊ตฌ ์‚ฌํ•ญ : ๊ธฐ์กด ํšŒ์› ๊ด€๋ฆฌ ๊ธฐ๋Šฅ + ํšŒ์› ์—ฐ๋ฝ์ฒ˜ ์ €์žฅ ์ถ”๊ฐ€

public class Member {
  
  private String memberId;
  private String name;
  private String tel; // ์ถ”๊ฐ€๋œ ํ•„๋“œ
  
}

1. ์—ฐ๋ฝ์ฒ˜ ์ €์žฅ INSERT SQL ์ˆ˜์ • ๋ฐ ๋“ฑ๋ก SQL์— ์ „๋‹ฌ String sql = "insert into member(member_id, name, tel) values (?, ?, ?); psmt.setString(3, member.getTel()); 2. ์กฐํšŒ ์ฝ”๋“œ ๋ณ€๊ฒฝ select member_id, name, tel from member where member_id = ? ... String tel = rs.getString("tel"); member.setTel(tel); // ์ถ”๊ฐ€ 3. ์ˆ˜์ • ์ฝ”๋“œ ๋ณ€๊ฒฝ 4. ์—ฐ๊ด€ ๊ฐ์ฒด Team ๊ฐ์ฒด๊ฐ€ Member ๊ฐ์ฒด์— ์ถ”๊ฐ€๋œ๋‹ค๋ฉด(์—ฐ๊ด€๋  ๋•Œ) member.getTeam().getTeamName(); โ†’ null ****select m.member_id, m.name, m.tel, m.team_id, t.team_name from member m from member m join team t on m.team_id = t.team_id โ†’ Member ๊ฐ์ฒด๊ฐ€ ์—ฐ๊ด€๋œ Team ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์„์ง€ ์—†์„์ง€๋Š” ์ „์ ์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” SQL์— ๋‹ฌ๋ ค์žˆ๋‹ค. ๋ฐ์ดํ„ฐ ์ ‘๊ทผ ๊ณ„์ธต์„ ์‚ฌ์šฉํ•ด SQL์„ ์ˆจ๊ฒจ๋„, ์–ด์ฉ” ์ˆ˜ ์—†์ด DAO๋ฅผ ์—ด์–ด ์–ด๋–ค SQL์ด ์‹คํ–‰๋˜๋Š”์ง€ ํ™•์ธ๋˜์–ด์•ผํ•œ๋‹ค.

๋ฌธ์ œ์ 

SQL์— ๋ชจ๋“  ๊ฒƒ์„ ์˜์กดํ•˜๋Š” ์ƒํ™ฉ์—์„œ, DAO๋ฅผ ์—ด์–ด ์–ด๋–ค SQL์ด ์‹คํ–‰๋˜๊ณ , ์–ด๋–ค ๊ฐ์ฒด๋“ค์ด ํ•จ๊ป˜ ์กฐํšŒ๋˜๋Š”์ง€ ํ™•์ธํ•ด์•ผ ํ•˜๋ฏ€๋กœ, ๊ฐœ๋ฐœ์ž๋“ค์ด ์—”ํ‹ฐํ‹ฐ(๋น„์ฆˆ๋‹ˆ์Šค ์š”๊ตฌ์‚ฌํ•ญ์„ ๋ชจ๋ธ๋งํ•œ ๊ฐ์ฒด)๋ฅผ ์‹ ๋ขฐํ•  ์ˆ˜ ์—†๋‹ค. ์ด ๊ฒƒ์€ ์ง„์ •ํ•œ ์˜๋ฏธ์˜ ๊ณ„์ธต ๋ถ„ํ• ์ด ์•„๋‹ˆ๋‹ค.

  1. ์ง„์ •ํ•œ ์˜๋ฏธ์˜ ๊ณ„์ธต ๋ถ„ํ• ์ด ์–ด๋ ต๋‹ค
  2. ์—”ํ‹ฐํ‹ฐ๋ฅผ ์‹ ๋ขฐํ•  ์ˆ˜ ์—†๋‹ค.
  3. SQL์— ์˜์กด์ ์ธ ๊ฐœ๋ฐœ์„ ํ”ผํ•  ์ˆ˜ ์—†๋‹ค.
  • DDD

    MDD

    Entity โ†’ Id (์‹๋ณ„ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฐ์ฒด)

    VO โ†’ ์‹๋ณ„ํ•  ์ˆ˜ ์žˆ์Œ. ๊ณต์œ ํ•˜๋Š” ์ฃผ์†Œ


    Service โ†’ ํ˜•์šฉ์‚ฌ? (MVC์˜ ์„œ๋น„์Šค์™€๋Š” ๋น„์Šทํ•˜๋ฉด์„œ ๋‹ค๋ฅธ?)

    Specification(๋ช…์„ธ) ?

    ์‚ฌ๋žŒ์ด ๋ฌธ์„ ์—ฐ๋‹ค โ†’ Service : ์šฐ๋ฆฌ ์ง์›

    ๋ช…์‚ฌ ๋ฌธ class ๋™์‚ฌ

      โ†’ open(Person)
    

3. JPA์˜ ๋ฌธ์ œ ํ•ด๊ฒฐ

CRUD ์ž‘์—…์„ ํ•  ๋•Œ, ๊ฐœ๋ฐœ์ž๊ฐ€ ์ง์ ‘ SQL์ด ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ JPA๊ฐ€ ์ œ๊ณตํ•˜๋Š” API๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.

๊ฐ์ฒด์™€ RDBMS์˜ ํŒจ๋Ÿฌ๋‹ค์ž„์˜ ๋ถˆ์ผ์น˜

๋ฐœ์ƒ ์ด์œ 

๊ฐ์ฒด์™€ ๊ด€๊ณ„ํ˜• ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์˜ ๋ชฉ์ , ๊ธฐ๋Šฅ, ํ‘œํ˜„๋ฐฉ๋ฒ•์ด ๋‹ค๋ฅด๋ฏ€๋กœ, ๊ฐ์ฒด ๊ตฌ์กฐ๋ฅผ ํ…Œ์ด๋ธ” ๊ตฌ์กฐ์— ์ €์žฅํ•˜๋Š” ๋ฐ ํ•œ๊ณ„๊ฐ€ ์žˆ๋‹ค.

๋ฌธ์ œ > ๋น„์ฆˆ๋‹ˆ์Šค ์š”๊ตฌ์‚ฌํ•ญ์„ ์ •์˜ํ•œ ๋„๋ฉ”์ธ ๋ชจ๋ธ์„ ์™ธ๋ถ€์— ์ €์žฅํ•  ๋•Œ ๋ฐœ์ƒ

๊ฐ์ฒด

๊ตฌ์กฐ - ์†์„ฑ(ํ•„๋“œ), ๊ธฐ๋Šฅ(๋ฉ”์„œ๋“œ)

RDBMS์˜ ๊ตฌ์กฐ

๋ฐ์ดํ„ฐ ์ค‘์‹ฌ์œผ๋กœ ๊ตฌ์กฐํ™”, ์ง‘ํ•ฉ์  ์‚ฌ๊ณ  ์š”๊ตฌ

๋ฌธ์ œ

1. ์ƒ์†

๊ฐ์ฒด๋Š” ์ƒ์†์ด๋ผ๋Š” ๊ธฐ๋Šฅ์ด ์žˆ์œผ๋‚˜ ํ…Œ์ด๋ธ”์€ ์ƒ์†์ด ์—†๋‹ค.

img

abstract class Item {
	Long id;
	String name;
	int price;
}

class Album extends Item {
	String artist;
}
class Movie extends Item {
	String director;
	String actor;
}
class Book extends Item {
	String author;
	String isbn;
}

๋งŒ์•ฝ, Album ๊ฐ์ฒด๋‚˜ Movie, Book ๊ฐ์ฒด๋ฅผ ์ €์žฅํ•˜๋ ค๋ฉด, Item๊ณผ ์ƒ์†๋ฐ›๋Š” ๊ฐ์ฒด๋ฅผ ๋ถ„ํ•ดํ•˜์—ฌ ๋‘ SQL์„ ๋งŒ๋“ค์–ด์•ผ ํ•œ๋‹ค.

insert into item ... 
insert into album ...

์กฐํšŒ ์—ญ์‹œ item๊ณผ album ํ…Œ์ด๋ธ”์„ ์กฐ์ธํ•˜์—ฌ ์กฐํšŒํ•œ ๋‹ค์Œ, ๊ทธ ๊ฒฐ๊ณผ๋ฅผ ํ†ตํ•ด Album ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•ด์•ผ ํ•œ๋‹ค. โ†’ ํŒจ๋Ÿฌ๋‹ค์ž„ ๋ถˆ์ผ์น˜ ํ•ด๊ฒฐ์„ ์œ„ํ•ด ์†Œ๋ชจํ•˜๋Š” ๋น„์šฉ

1-1. JPA์™€ ์ƒ์†

JPA๋Š” ์œ„์™€๊ฐ™์€ ์ƒ์† ๊ด€๋ จ ํŒจ๋Ÿฌ๋‹ค์ž„ ๋ถˆ์ผ์น˜ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•œ๋‹ค.

jpa.persist(album);

persist() ๋ฉ”์†Œ๋“œ๋ฅผ ์‹คํ–‰ํ•˜๋ฉด ์œ„์—์„œ์˜ ๋ฒˆ๊ฑฐ๋กœ์šด ๊ณผ์ •์„ JPA๊ฐ€ ๋Œ€์‹  ํ•ด์ค€๋‹ค.

2. ์—ฐ๊ด€๊ด€๊ณ„

๊ฐ์ฒด๋Š” ์ฐธ์กฐ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์—ฐ๊ด€๊ด€๊ณ„๋ฅผ ๊ฐ€์ง€๊ณ , ์ฐธ์กฐ์— ์ ‘๊ทผํ•˜์—ฌ ์—ฐ๊ด€๋œ ๊ฐ์ฒด๋ฅผ ์กฐํšŒํ•œ๋‹ค.

ํ…Œ์ด๋ธ”์€ ์™ธ๋ž˜ ํ‚ค๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋‹ค๋ฅธ ํ…Œ์ด๋ธ”๊ณผ ์—ฐ๊ด€๊ด€๊ณ„๋ฅผ ๊ฐ€์ง€๊ณ , ์กฐ์ธ์„ ์‚ฌ์šฉํ•ด์„œ ์—ฐ๊ด€๋œ ํ…Œ์ด๋ธ”์„ ์กฐํšŒํ•œ๋‹ค.

img2

์ฐธ์กฐ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฐ์ฒด์™€ ์™ธ๋ž˜ ํ‚ค๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ด€๊ณ„ํ˜• ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์‚ฌ์ด์˜ ํŒจ๋Ÿฌ๋‹ค์ž„ ๋ถˆ์ผ์น˜๋Š”, ๊ฐ์ฒด์ง€ํ–ฅ ๋ชจ๋ธ๋ง์„ ํฌ๊ธฐํ•˜๊ฒŒ ๋งŒ๋“ค ์ •๋„๋กœ ๊ทน๋ณตํ•˜๊ธฐ ์–ด๋ ต๋‹ค.

๊ฐ์ฒด >

Member์€ Member.team ํ•„๋“œ์—, Team ๊ฐ์ฒด์˜ ์ฐธ์กฐ๋ฅผ ๋ณด๊ด€ํ•˜์—ฌ, Team ๊ฐ์ฒด์™€ ๊ด€๊ณ„๋ฅผ ๋งบ๋Š”๋‹ค. ๋”ฐ๋ผ์„œ ์ด ์ฐธ์กฐ ํ•„๋“œ์— ์ ‘๊ทผํ•˜๋ฉด Member์™€ ์—ฐ๊ด€๋œ Team์„ ์กฐํšŒํ•  ์ˆ˜ ์žˆ๋‹ค.

ํ…Œ์ด๋ธ” >

MEMBER ํ…Œ์ด๋ธ”์€ MEMBER.TEAM_ID ์™ธ๋ž˜ ํ‚ค ์ปฌ๋Ÿผ์„ ์‚ฌ์šฉํ•˜์—ฌ, TEAM ํ…Œ์ด๋ธ”๊ณผ ๊ด€๊ณ„๋ฅผ ๋งบ๋Š”๋‹ค.

์ด ์™ธ๋ž˜ํ‚ค๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ MEMBER ํ…Œ์ด๋ธ”๊ณผ TEAM ํ…Œ์ด๋ธ”์„ ์กฐ์ธํ•˜๋ฉด MEMBER ํ…Œ์ด๋ธ”๊ณผ ์—ฐ๊ด€๋œ TEAM ํ…Œ์ด๋ธ”์„ ์กฐํšŒํ•  ์ˆ˜ ์žˆ๋‹ค.

์ฐจ์ด์  >

team.getMember()๋Š” ๋ถˆ๊ฐ€๋Šฅํ•˜์ง€๋งŒ, ์™ธ๋ž˜ํ‚ค ํ•˜๋‚˜๋กœ TEAM JOIN MEMBER๋Š” ๊ฐ€๋Šฅํ•˜๋‹ค.

2.1 ๊ฐ์ฒด๋ฅผ ํ…Œ์ด๋ธ”์— ๋งž์ถ”์–ด ๋ชจ๋ธ๋ง

class Member {
	String id;
	Long teamId; // TEAM_ID FK
	String username; 
}

class Team {
	Long id;
	String name;
}

์œ„์™€ ๊ฐ™์ด ๊ฐ์ฒด๋ฅผ ํ…Œ์ด๋ธ”์— ๋งž์ถ”์–ด ๋ชจ๋ธ๋งํ•˜๋ฉด, ๊ฐ์ฒด๋ฅผ ํ…Œ์ด๋ธ”์— ์ €์žฅํ•˜๊ฑฐ๋‚˜ ์กฐํšŒํ•  ๋•Œ๋Š” ํŽธ๋ฆฌํ•˜์ง€๋งŒ, ๊ฐ์ฒด์ง€ํ–ฅ์ ์ธ ๋ฐฉ๋ฒ•์€ ์•„๋‹ˆ๋‹ค.

๊ฐ์ฒด๋Š” ์—ฐ๊ด€๋œ ์ฐธ์กฐ๋ฅผ ๋ณด๊ด€ํ•ด์•ผ, ์ฐธ์กฐ๋ฅผ ํ†ตํ•ด ์—ฐ๊ด€๋œ ๊ฐ์ฒด๋ฅผ ์ฐพ์„ ์ˆ˜ ์žˆ๋‹ค.

Team team = member.getTeam();

์ด๋Ÿฌํ•œ ์ฐธ์กฐ๋ฅผ ํ†ตํ•œ ์กฐํšŒ๊ฐ€ ๊ฐ€์žฅ ๊ฐ์ฒด์ง€ํ–ฅ์ ์ธ ๋ฐฉ๋ฒ•์ด๋‹ค.

2.2 ๊ฐ์ฒด์ง€ํ–ฅ์  ๋ชจ๋ธ๋ง

class Member {
	String id;
	Team team;
	String username;
	
	Team getTeam() {
		return team;
	}
}

class Team {
	Long id;
	String name;
}

Member.team์˜ ํ•„๋“œ๋Š” ์™ธ๋ž˜ ํ‚ค์˜ ๊ฐ’์„ ๊ทธ๋Œ€๋กœ ๋ณด๊ด€ํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹Œ, Team์˜ ์ฐธ์กฐ๋ฅผ ๋ณด๊ด€ํ•œ๋‹ค,

์ด์ฒ˜๋Ÿผ ๊ฐ์ฒด์ง€ํ–ฅ ๋ชจ๋ธ๋ง์„ ์‚ฌ์šฉํ•˜๋ฉด, ๊ฐ์ฒด๋ฅผ ํ…Œ์ด๋ธ”์— ์ €์žฅ or ์กฐํšŒ ํ•˜๋Š” ๊ฒƒ์€ ์‰ฝ์ง€ ์•Š๋‹ค.

2.3 JPA์™€ ์—ฐ๊ด€๊ด€๊ณ„

JPA๋Š” ์—ฐ๊ด€๊ด€๊ณ„์™€ ๊ด€๋ จ๋œ ํŒจ๋Ÿฌ๋‹ค์ž„์˜ ๋ถˆ์ผ์น˜ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐ

: ๊ฐ์ฒด์˜ ์ฐธ์กฐ โ†’ ์™ธ๋ž˜ํ‚ค๋กœ ๋ณ€ํ™˜

3. ๊ฐ์ฒด ๊ทธ๋ž˜ํ”„ ํƒ์ƒ‰

๊ฐ์ฒด ๊ทธ๋ž˜ํ”„ ํƒ์ƒ‰์ด๋ž€?

Team team = member.getTeam();

MemberDAO์—์„œ ํšŒ์›๊ณผ ํŒ€์— ๋Œ€ํ•ด์„œ๋งŒ ๋ฐ์ดํ„ฐ๋ฅผ ์กฐํšŒํ•œ ๊ฒฝ์šฐ, member.getTeam()์— ๋Œ€ํ•œ ์กฐํšŒ๋Š” ์„ฑ๊ณตํ•˜์ง€๋งŒ, member.getOrder()๋Š” ์‹คํŒจํ•œ๋‹ค.

SQL์„ ์ง์ ‘ ๋‹ค๋ฃจ๋ฉด, ์ฒ˜์Œ ์‹คํ–‰ํ•˜๋Š” SQL์— ๋”ฐ๋ผ ๊ฐ์ฒด ๊ทธ๋ž˜ํ”„๋ฅผ ์–ด๋””๊นŒ์ง€ ํƒ์ƒ‰ํ•  ์ˆ˜ ์žˆ๋Š”์ง€ ์ •ํ•ด์ง„๋‹ค. โ†’ ๊ฐ์ฒด์ง€ํ–ฅ์—์„œ๋Š” ์ œ์•ฝ

  • ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง์— ๋”ฐ๋ผ ์‚ฌ์šฉํ•˜๋Š” ๊ฐ์ฒด ๊ทธ๋ž˜ํ”„๊ฐ€ ๋‹ค๋ฆ„
  • ์–ธ์ œ ๋Š์–ด์งˆ์ง€ ๋ชจ๋ฅด๋Š” ๊ฐ์ฒด ๊ทธ๋ž˜ํ”„๋ฅผ ํ•จ๋ถ€๋กœ ํƒ์ƒ‰ํ•  ์ˆ˜ ์—†๋‹ค.

3-1. JPA์™€ ๊ฐ์ฒด ๊ทธ๋ž˜ํ”„ ํƒ์ƒ‰

JPA๋Š” ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์‹œ์ ์— ์ ์ ˆํ•œ SELECT SQL์„ ์‹คํ–‰

  • ์—ฐ๊ด€๋œ ๊ฐ์ฒด 1) ์ฆ‰์‹œ ์กฐํšŒ(Eager Loading), 2) ์‹ค์ œ ์‚ฌ์šฉํ•˜๋Š” ์‹œ์ ์— ์ง€์—ฐ ์กฐํšŒ(Lazy Loading)ํ•  ์ง€ ์„ค์ •์„ ํ†ตํ•ด ์ •์˜

4. ๋น„๊ต

DB โ†’ PK๋ฅผ ํ†ตํ•ด ๊ฐ row๊ตฌ๋ถ„

๊ฐ์ฒด โ†’ ๋™์ผ์„ฑ, ๋™๋“ฑ์„ฑ ๋น„๊ต

  • ๋™์ผ์„ฑ ๋น„๊ต๋Š” == ๋น„๊ต โ†’ ๊ฐ์ฒด ์ธ์Šคํ„ด์Šค ์ฃผ์†Œ ๊ฐ’ ๋น„๊ต
  • ๋™๋“ฑ์„ฑ ๋น„๊ต โ†’ equals()๋ฅผ ํ†ตํ•œ ๊ฐ์ฒด ๋‚ด๋ถ€์˜ ๊ฐ’ ๋น„๊ต
Member member1 = memberDAO.getMember(memberId);
Member member2 = memberDAO.getMember(memberId);

member1 โ‰  member2

4-1. JPA์™€ ๋น„๊ต

JPA๋Š” ๊ฐ™์€ ํŠธ๋žœ์žญ์…˜์ผ ๋•Œ ๊ฐ™์€ ๊ฐ์ฒด๊ฐ€ ์กฐํšŒ๋˜๋Š” ๊ฒƒ์„ ๋ณด์žฅ

๋”ฐ๋ผ์„œ member1 = member2

JPA๋ž€ ๋ฌด์—‡์ธ๊ฐ€?

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/ff2d8e09-cdfb-4f09-a5cd-91f91dc7f23d/IMG_2068.jpg

JPA(Java Persistence API)๋Š” ์ž๋ฐ”์˜ ORM ๊ธฐ์ˆ  ํ‘œ์ค€์ด๋‹ค. ORM์€, Object-Relational Mapping ์ด๋ฆ„ ๊ทธ๋Œ€๋กœ ๊ฐ์ฒด โ†” RDBMS๋ฅผ ๋งคํ•‘ํ•œ๋‹ค๋Š” ๋œป์ด๋‹ค. ORM ํ”„๋ ˆ์ž„์›Œํฌ๋Š” ์œ„์™€ ๊ฐ™์ด ๊ฐ์ฒด์™€ ํ…Œ์ด๋ธ”์„ ๋งคํ•‘ํ•˜์—ฌ ๋‘˜ ์‚ฌ์ด์˜ ํŒจ๋Ÿฌ๋‹ค์ž„ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•ด์ค€๋‹ค.

JPA ์†Œ๊ฐœ

ํ•˜์ด๋ฒ„๋„ค์ดํŠธ

์ž๋ฐ” ์ง„์˜์˜ ์˜คํ”ˆ์†Œ์Šค ORM ํ”„๋ ˆ์ž„์›Œํฌ

EJB์˜ ORM์ด์—ˆ๋˜ ์—”ํ‹ฐํ‹ฐ ๋นˆ๊ณผ ๋น„๊ตํ–ˆ์„ ๋•Œ, ๊ฐ€๋ณ๊ณ  ์‹ค์šฉ์ ์ด๋ฉฐ ๊ธฐ์ˆ  ์„ฑ์ˆ™๋„๋„ ๋†’์Œ, ์ž๋ฐ” ์—”ํ„ฐํ”„๋ผ์ด์ฆˆ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์„œ๋ฒ„ ์—†์ด๋„ ๋™์ž‘

โ†’ EJB 3.0์—์„œ ํ•˜์ด๋ฒ„๋„ค์ดํŠธ ๊ธฐ๋ฐ˜์œผ๋กœ ์ƒˆ๋กœ์šด ์ž๋ฐ” ORM ํ‘œ์ค€ ํƒ„์ƒ โ‡’ JPA!

img3

JPA๋Š” ์ž๋ฐ” ORM ๊ธฐ์ˆ ์— ๋Œ€ํ•œ ํ‘œ์ค€ ๋ช…์„ธ

โ†’ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๋ชจ์•„๋‘” ๊ฒƒ

โ†’ ์ด ์ธํ„ฐํŽ˜์ด์Šค๋“ค์„ ๊ตฌํ˜„ํ•œ ํ”„๋ ˆ์ž„์›Œํฌ๊ฐ€ ํ•˜์ด๋ฒ„๋„ค์ดํŠธ

JPA๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๋Š” ์ด์œ 

์ƒ์‚ฐ์„ฑ

์ง€๋ฃจํ•˜๊ณ  ๋ฐ˜๋ณต์ ์ธ ์ฝ”๋“œ์™€ CRUD์šฉ SQL์„ ๊ฐœ๋ฐœ์ž๊ฐ€ ์ง์ ‘ ์ž‘์„ฑํ•˜์ง€ ์•Š์•„๋„ O

๊ฐ์ฒด ์„ค๊ณ„ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ ๊ฐ€๋Šฅ

์œ ์ง€๋ณด์ˆ˜

๊ธฐ์กด์—๋Š” ์—”ํ‹ฐํ‹ฐ์— ํ•„๋“œ ํ•˜๋‚˜๋งŒ ์ถ”๊ฐ€ํ•ด๋„ CRUD SQL๊ณผ ๊ฒฐ๊ณผ๋ฅผ ๋งคํ•‘ํ•˜๊ธฐ ์œ„ํ•œ JDBC API ์ฝ”๋“œ๋ฅผ ์ „๋ถ€ ๋ณ€๊ฒฝํ•ด์•ผ ํ–ˆ์ง€๋งŒ, JPA๋Š” ๋Œ€์‹  ์ฒ˜๋ฆฌํ•ด์ฃผ์–ด ์ˆ˜์ •ํ•ด์•ผํ•  ์ฝ”๋“œ๊ฐ€ ์ค„์–ด๋“ ๋‹ค

ํŒจ๋Ÿฌ๋‹ค์ž„์˜ ๋ถˆ์ผ์น˜ ํ•ด๊ฒฐ

์„ฑ๋Šฅ

JPA ์—†์ด๋Š” ๊ฐ™์€ ํŠธ๋žœ์žญ์…˜ ์•ˆ์—์„œ ๊ฐ™์€ ์ฝ”๋“œ๋ฅผ ๋‘ ๋ฒˆ ์กฐํšŒ ํ–ˆ์–ด์•ผ ํ–ˆ์ง€๋งŒ, JPA๋ฅผ ํ†ตํ•ด ํ•œ ๋ฒˆ๋งŒ DB์— ์กฐํšŒํ•œ ๋’ค, ๊ทธ ๋’ค๋กœ๋Š” ๊ธฐ์กด์— ์กฐํšŒํ•œ ๊ฐ์ฒด ์‚ฌ์šฉ ๊ฐ€๋Šฅ

๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ ‘๊ทผ ์ถ”์ƒํ™”์™€ ๋ฒค๋” ๋…๋ฆฝ์„ฑ

RDBMS๋Š” ๊ฐ™์€ ๊ธฐ๋Šฅ๋„ ๋ฒค๋”๋งˆ๋‹ค ์‚ฌ์šฉ๋ฒ•์ด ๋‹ค๋ฅด๋‹ค.

ํ•˜์ง€๋งŒ JPA๋Š” ํŠน์ • DB ๊ธฐ์ˆ ์— ์ข…์†๋˜์ง€ X

๋ฐฉ์–ธ ์„ค์ •์„ ํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

ํ‘œ์ค€