๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

Spring๐Ÿƒ

JPA : ORM ์ด์ „๊นŒ์ง€ ํ๋ฆ„

728x90

์ถœ์ฒ˜ : ๋‚ด์ผ๋ฐฐ์›€์บ ํ”„

 

๋ณด๋‹ค ์ž์„ธํ•˜๊ฒŒ ์ •๋ฆฌ :  https://cdaosldk.tistory.com/167

 

ORM์˜ ํƒ„์ƒ ๋ฐฐ๊ฒฝ(JDBC, Querry Mapper)

์ถœ์ฒ˜ : ๋‚ด์ผ๋ฐฐ์›€์บ ํ”„ ๋ณด๋‹ค ๊ฐ„๋‹จํ•˜๊ฒŒ ์ •๋ฆฌํ•œ ๊ฒƒ : https://cdaosldk.tistory.com/69 1. DB ํƒ„์ƒ ํ›„ JAVA ์•ฑ๊ณผ ์—ฐ๋™์„ ์œ„ํ•ด JDBC ์ถœ์‹œ JDBC Driver Manager๋Š” ์—ฌ๋Ÿฌ ํƒ€์ž…์˜ DB์™€ ์—ฐ๊ฒฐํ•  ์ˆ˜ ์žˆ๋Š” ๊ธฐ๋Šฅ์„ ์ œ๊ณต JDBC Driver Manage

cdaosldk.tistory.com

์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ์ง์ ‘ ๋‹ค๋ฃฐ ๋•Œ์˜ ๋ฌธ์ œ์ 

1. ๋ฒˆ๊ฑฐ๋กœ์›€

- ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ํ…Œ์ด๋ธ” ๋งŒ๋“ค๊ธฐ ->

create table user (
	id int not null,
	name varchar(20) not null,
	age int not null, address char(25),
	salary int,
	primary key(id)
	);

์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ ์ฟผ๋ฆฌ ๋งŒ๋“ค๊ธฐ ->

String userSelectQuery = "select name, age, salary from user u where u.name = "

String userName = request.getHeader("name");

String needToExecuteQuery = userSelectQuery + userName;

APP์—์„œ jdbcAPI๋ฅผ ํ†ตํ•œ ์ฟผ๋ฆฌ์‹คํ–‰ ->

ResultSet result = jdbcAPI.executeQuery(needToExecuteQuery);

 

์ฟผ๋ฆฌ ๊ฒฐ๊ณผ๋ฅผ ๊ฐ์ฒด์— ํ• ๋‹นํ•˜๊ธฐ ์œ„ํ•ด ์ง์ ‘ ๊ฐ์ฒด๋„ ๋งŒ๋“ค์–ด์•ผ ํ•œ๋‹ค.

User user = new User();

user.setId(result.id);
user.setName(result.name);
user.setAge(result.age);
user.setSalary(result.salary);

 

2. SQL ์˜์กด์ ์ด๋ผ ๋ณ€๊ฒฝ์— ์ทจ์•ฝํ•˜๋‹ค

๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ํ•„๋“œ ์ถ”๊ฐ€ ์‹œ, ๋ณ€๊ฒฝํ•ด์•ผํ•  ๊ฒƒ๋“ค:

APP, ์ฟผ๋ฆฌ๋ฌธ, ๊ฐ์ฒด ๊ฐ’ ํ• ๋‹น

 

3. ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์™€ ์„œ๋ฒ„ ๊ฐ„ ์ฒด๊ณ„ ๋ถˆ์ผ์น˜

  ๊ฐ์ฒด(APP) ๋ฆด๋ ˆ์ด์…˜(๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค)
๋ฐ€๋„ ๋‹ค์–‘ํ•œ ํฌ๊ธฐ, ์ž์œ ๋กœ์šด ์ปค์Šคํ…€ ํƒ€์ž… ๊ธฐ๋ณธ ๋ฐ์ดํ„ฐ ํƒ€์ž…
์„œ๋ธŒ ํƒ€์ž… ์ƒ์† ๊ฐ€๋Šฅ, ๋‹คํ˜•์„ฑ ๊ตฌํ˜„ ์‰ฌ์›€ ์ƒ์† ๋ถˆ๊ฐ€๋Šฅ, ๋‹คํ˜•์„ฑ ๊ตฌํ˜„ ๋ถˆ๊ฐ€
์‹๋ณ„์„ฑ ๋ ˆํผ๋Ÿฐ์Šค(==), ์ธ์Šคํ„ด์Šค(equals) ๋™์ผ์„ฑ primary key only
๊ด€๊ณ„ ์„œ๋กœ ๊ฐ„ ๊ฐ์ฒด ์ฐธ์กฐ๋ฅผ ํ†ตํ•ด ํ‘œํ˜„,
๋‹ค๋Œ€๋‹ค ๊ด€๊ณ„ํ˜•์„ฑ ๊ฐ€๋Šฅ,
๋ฐฉํ–ฅ์ด ์žˆ๋‹ค
๋‹ค๋Œ€๋‹ค ๊ด€๊ณ„ํ˜•์„ฑ ๋ถˆ๊ฐ€๋Šฅํ•ด ํ…Œ์ด๋ธ”๋กœ ์ฒ˜๋ฆฌ, ์™ธ๋ž˜ ํ‚ค๋กœ ๋ฐ”๋กœ ์กฐํšŒ(๋ฐฉํ–ฅ์—†์Œ)
๋ฐ์ดํ„ฐ ๋„ค๋น„๊ฒŒ์ด์…˜ ๋งˆ์Œ๋Œ€๋กœ ๋ ˆํผ๋Ÿฐ์Šค ๋”ฐ๋ผ ์ด๋™ ๊ฐ€๋Šฅ ๋น„ํšจ์œจ์ ์ธ JOIN, ์„ฑ๋Šฅ ์ €ํ•˜ ๋ฐœ์ƒ

*๊ด€๊ณ„

class Member {
	private int memberId;
	private String mamberName;
	private Team team;

}

class Team {
	private int teamId;
	private String teamName;
}


Member member = new Member();
Team team = new Team();

member.setTeam(team);

member.team(); //๊ฐ€๋Šฅ
team.member(); // ๋ถˆ๊ฐ€๋Šฅ
// ๋ฉค๋ฒ„ ํด๋ž˜์Šค์™€ ํŒ€ ํด๋ž˜์Šค๊ฐ€ ์žˆ์„๋•Œ, ๋ฉค๋ฒ„ ํด๋ž˜์Šค ์•ˆ์—๋Š” ํŒ€ ๊ฐ์ฒด๊ฐ€ ์žˆ๋‹ค.
// ๊ฐ ํด๋ž˜์Šค๋ฅผ ์ธ์Šคํ„ด์Šคํ™”(๋ณ€์ˆ˜์— ํด๋ž˜์Šค ์†์„ฑ์„ ํ• ๋‹น) ํ–ˆ์„๋•Œ, ๋ณ€์ˆ˜ member์— ์†์„ฑ team์„ ํ• ๋‹นํ•  ์ˆ˜ ์žˆ๋‹ค.
// ๋ณ€์ˆ˜ member๋ฅผ ํ†ตํ•ด ๋ณ€์ˆ˜ team(ํด๋ž˜์Šค team)์„ ํ˜ธ์ถœํ•  ์ˆ˜ ์žˆ์ง€๋งŒ, ๋ฐ˜๋Œ€์˜ ๊ฒฝ์šฐ๋Š” ๋ถˆ๊ฐ€๋Šฅ
// -> member๋ฅผ ํ†ตํ•ด team ์†์„ฑ์„ ์กฐํšŒํ•  ์ˆ˜ ์žˆ๋‹ค.

// + ๊ฐ•์˜๋ณด๊ณ  ์ดํ•ด ํ™•์žฅํ•œ ํ›„ ์ ๊ธฐ

ORM : ๊ฐ์ฒด ๊ด€๊ณ„ ๋งคํ•‘ ๊ธฐ์ˆ 

JPA! : ์ž๋ฐ” ORM ๊ธฐ์ˆ ์— ๋Œ€ํ•œ ํ‘œ์ค€ ๋ช…์„ธ (JAVA Persistence API)

JPA๋Š” ๊ธฐ์กด์˜ ๋ฐ˜๋ณต ์ฝ”๋“œ๋Š” ๋ฌผ๋ก ์ด๊ณ , ๊ธฐ๋ณธ์ ์ธ SQL๋„ JPA๊ฐ€ ์ง์ ‘ ๋งŒ๋“ค์–ด์„œ ์‹คํ–‰ํ•ด์ค€๋‹ค.

JPA๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด, SQL๊ณผ ๋ฐ์ดํ„ฐ ์ค‘์‹ฌ์˜ ์„ค๊ณ„์—์„œ ๊ฐ์ฒด ์ค‘์‹ฌ์˜ ์„ค๊ณ„๋กœ ํŒจ๋Ÿฌ๋‹ค์ž„์„ ์ „ํ™˜์„ ํ•  ์ˆ˜ ์žˆ๋‹ค.

JPA๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๊ฐœ๋ฐœ ์ƒ์‚ฐ์„ฑ์„ ํฌ๊ฒŒ ๋†’์ผ ์ˆ˜ ์žˆ๋‹ค.

 

JPA ์ƒ˜ํ”Œ

@Entity // DB ํ…Œ์ด๋ธ” ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค.
public class User {
		// ID๊ฐ€ ์ž๋™์œผ๋กœ ์ƒ์„ฑ ๋ฐ ์ฆ๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.
	  @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    // nullable: null ํ—ˆ์šฉ ์—ฌ๋ถ€
    // unique: ์ค‘๋ณต ํ—ˆ์šฉ ์—ฌ๋ถ€ (false ์ผ๋•Œ ์ค‘๋ณต ํ—ˆ์šฉ)
    @Column(nullable = false, unique = true)
    private String username;

    @Column(nullable = false)
    private String password;

    @Column(nullable = false, unique = true)
    private String email;

    @Column(nullable = false)
    @Enumerated(value = EnumType.STRING)
    private UserRoleEnum role;

    @Column(unique = true)
    private Long kakaoId;
}

 

DB ์—ฐ๊ด€๊ด€๊ณ„ ์˜ˆ์ œ

 

create table member(
    id bigint auto_increment comment '๋ฒˆํ˜ธ',
    customer_name varchar(100) comment '๊ณ ๊ฐ ์ด๋ฆ„',
)
insert into member(CUSTOMER_NAME) values ('์‚ผ์‹์ด');
insert into member(CUSTOMER_NAME) values ('๋จน๊นจ๋น„');
create table food(
    id bigint auto_increment comment '๋ฒˆํ˜ธ',
    name varchar(100) comment '๋ฉ”๋‰ด',
    price bigint comment '๊ฐ€๊ฒฉ'
)
insert into FOOD(name, price) values ( 'ํ›„๋ผ์ด๋“œ', 10000 );
insert into FOOD(name, price) values ( '์–‘๋…', 12000 );
insert into FOOD(name, price) values ( '๋ฐ˜๋ฐ˜', 13000 );
insert into FOOD(name, price) values ( '๊ณ ๊ตฌ๋งˆํ”ผ์ž', 9000 );
insert into FOOD(name, price) values ( '์•„๋ณด์นด๋„ํ”ผ์ž', 110000 );

*๊ณ ๊ฐ์˜ ์Œ์‹์ฃผ๋ฌธ

1) ๊ณ ๊ฐํ…Œ์ด๋ธ”์— ์ถ”๊ฐ€

-> ์Œ์‹ ์ฃผ๋ฌธ์‹œ๋งˆ๋‹ค ๊ณ ๊ฐ ํ…Œ์ด๋ธ”์— ์ค‘๋ณต๋œ ๊ณ ๊ฐ์ •๋ณด๋ฅผ ๋“ฑ๋กํ•ด์•ผ ํ•œ๋‹ค.

2) ์Œ์‹ํ…Œ์ด๋ธ”์— ์ถ”๊ฐ€

-> ๋™์ผ ์Œ์‹ ์ฃผ๋ฌธ์ด ์ค‘๋ณต์œผ๋กœ ์ƒˆ๋กœ์šด ๊ฐ’์ด ๋“ค์–ด๊ฐˆ ์ˆ˜ ์—†๋‹ค.

3) ์ฃผ๋ฌธ์„ ์œ„ํ•œ ์˜ค๋”ํ…Œ์ด๋ธ”์ด ์žˆ์–ด์•ผ ํ•œ๋‹ค :

๋ฉค๋ฒ„ํ…Œ์ด๋ธ”๊ณผ ์Œ์‹ํ…Œ์ด๋ธ”์˜ id๋ฅผ ๊ฐ€์ ธ์™€ ์ฃผ๋ฌธํ…Œ์ด๋ธ”์— ํ–‰์„ ์ถ”๊ฐ€ํ•˜์—ฌ ์ฃผ๋ฌธ์ •๋ณด์ €์žฅ

 

 

728x90