Home > Spring > ๐Ÿƒ[Spring] Spring Data JPA๋ž€ ๋ฌด์—‡์ผ๊นŒ์š”?

๐Ÿƒ[Spring] Spring Data JPA๋ž€ ๋ฌด์—‡์ผ๊นŒ์š”?
Spring Framework

๐Ÿƒ[Spring] Spring Data JPA๋ž€ ๋ฌด์—‡์ผ๊นŒ์š”?

  • Spring Data JPA๋Š” Spring Framework์˜ ์ผ๋ถ€๋กœ, JPA(Java Persistence API)๋ฅผ ๋”์šฑ ์‰ฝ๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ๋„์™€์ฃผ๋Š” ๋ชจ๋“ˆ(Module)์ž…๋‹ˆ๋‹ค.
  • JPA(Java Persistence API)๋Š” ์ž๋ฐ” ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ ๊ฐ์ฒด์™€ ๊ด€๊ณ„ํ˜• ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค(Relational Database, RDB) ๊ฐ„์˜ ๋งคํ•‘์„ ์ œ๊ณตํ•˜๋Š” ํ‘œ์ค€ ์ธํ„ฐํŽ˜์ด์Šค์ธ๋ฐ, Spring Data JPA๋Š” ์ด๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ๊ฐœ๋ฐœ์ž๊ฐ€ ๋” ์ ์€ ์ฝ”๋“œ๋กœ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์™€ ์ƒํ˜ธ์ž‘์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋„์™€์ค๋‹ˆ๋‹ค.

1๏ธโƒฃ Spring Data JPA์˜ ์ฃผ์š” ํŠน์ง•.

1๏ธโƒฃ ๊ฐ„ํŽธํ•œ ๋ฐ์ดํ„ฐ ์—‘์„ธ์Šค ๊ณ„์ธต.

  • Spring Data JPA๋Š” ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์™€์˜ CRUD(Create, Read, Update, Delete) ์ž‘์—…์„ ์†์‰ฝ๊ฒŒ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.
  • ๊ฐœ๋ฐœ์ž๋Š” ๋ณต์žกํ•œ SQL ์ฟผ๋ฆฌ๋ฅผ ์ง์ ‘ ์ž‘์„ฑํ•  ํ•„์š” ์—†์ด, Repository ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ •์˜ํ•˜๋Š” ๊ฒƒ๋งŒ์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ž‘์—…์„ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

2๏ธโƒฃ ์ž๋™ ๊ตฌํ˜„ Repository.

  • Spring Data JPA๋Š” Repository ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ •์˜ํ•˜๋ฉด, ๋Ÿฐํƒ€์ž„ ์‹œ์ ์— ํ•ด๋‹น ์ธํ„ฐํŽ˜์ด์Šค์˜ ๊ตฌํ˜„์ฒด๋ฅผ ์ž๋™์œผ๋กœ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
  • ์˜ˆ๋ฅผ ๋“ค์–ด, findById, save, delete์™€ ๊ฐ™์€ ๊ธฐ๋ณธ์ ์ธ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์—ฐ์‚ฐ์€ ๋ณ„๋„๋กœ ๊ตฌํ˜„ํ•  ํ•„์š”๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.

3๏ธโƒฃ ๋ฉ”์„œ๋“œ ์ด๋ฆ„ ๊ธฐ๋ฐ˜ ์ฟผ๋ฆฌ ์ƒ์„ฑ.

  • Spring Data JPA๋Š” ๋ฉ”์„œ๋“œ ์ด๋ฆ„์„ ๋ถ„์„ํ•˜์—ฌ ์ž๋™์œผ๋กœ ์ฟผ๋ฆฌ๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
    • ์˜ˆ๋ฅผ ๋“ค์–ด, findByName๊ณผ ๊ฐ™์€ ๋ฉ”์„œ๋“œ๋ฅผ ์ •์˜ํ•˜๋ฉด, name ํ•„๋“œ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์กฐํšŒํ•˜๋Š” ์ฟผ๋ฆฌ๋ฅผ ์ž๋™์œผ๋กœ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
  • ์ด๋ฅผ ํ†ตํ•ด ๋ณต์žกํ•œ ์ฟผ๋ฆฌ๋„ ์‰ฝ๊ฒŒ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์ถ”๊ฐ€์ ์ธ ์ฝ”๋“œ ์ž‘์„ฑ์ด ์ค„์–ด๋“ค์–ด ๊ฐœ๋ฐœ ์†๋„๋ฅผ ๋†’์—ฌ์ค๋‹ˆ๋‹ค.

4๏ธโƒฃ JPQL ๋ฐ ๋„ค์ดํ‹ฐ๋ธŒ ์ฟผ๋ฆฌ ์ง€์›.

  • ํ•„์š”ํ•  ๊ฒฝ์šฐ JPQL(Java Persistence Query Language) ๋˜๋Š” ๋„ค์ดํ‹ฐ๋ธŒ SQL(Structured Query Language) ์ฟผ๋ฆฌ๋ฅผ ์ง์ ‘ ์ž‘์„ฑํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.
    • ๋ณต์žกํ•œ ์ฟผ๋ฆฌ๋ฅผ ์ฒ˜๋ฆฌํ•˜๊ฑฐ๋‚˜ ์„ฑ๋Šฅ ์ตœ์ ํ™”๊ฐ€ ํ•„์š”ํ•œ ๊ฒฝ์šฐ ์œ ์šฉํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

5๏ธโƒฃ ํŽ˜์ด์ง• ๋ฐ ์ •๋ ฌ ์ง€์›.

  • Spring Data JPA๋Š” ๋ฐ์ดํ„ฐ ์กฐํšŒ ์‹œ ํŽ˜์ด์ง•(Paging)๊ณผ ์ •๋ ฌ(Sorting) ๊ธฐ๋Šฅ์„ ๊ธฐ๋ณธ์ ์œผ๋กœ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค.
    • ์ด๋ฅผ ํ†ตํ•ด ๋Œ€๋Ÿ‰์˜ ๋ฐ์ดํ„ฐ๋ฅผ ํšจ์œจ์ ์œผ๋กœ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

2๏ธโƒฃ ์ฃผ์š” ๊ตฌ์„ฑ ์š”์†Œ.

1๏ธโƒฃ Entity ํด๋ž˜์Šค.

  • ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ํ…Œ์ด๋ธ”๊ณผ ๋งคํ•‘๋˜๋Š” ์ž๋ฐ” ํด๋ž˜์Šค์ž…๋‹ˆ๋‹ค.
    • JPA(Java Persistence API) ์—”ํ‹ฐํ‹ฐ(Entity) ์–ด๋…ธํ…Œ์ด์…˜(@Entity)์„ ์‚ฌ์šฉํ•˜์—ฌ ํ…Œ์ด๋ธ” ๊ตฌ์กฐ๋ฅผ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค.

2๏ธโƒฃ Repotitory ์ธํ„ฐํŽ˜์ด์Šค.

  • Spring Data JPA์—์„œ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์ ‘๊ทผํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•˜๋Š” ์ธํ„ฐํŽ˜์ด์Šค์ž…๋‹ˆ๋‹ค.
  • CrudRepository, JpaRepository, PagingAndSortingRepository์™€ ๊ฐ™์€ ๋‹ค์–‘ํ•œ Repository ์ธํ„ฐํŽ˜์ด์Šค๊ฐ€ ์ œ๊ณต๋ฉ๋‹ˆ๋‹ค.
    • ์ด๋ฅผ ํ™•์žฅํ•˜์—ฌ ๊ธฐ๋ณธ์ ์ธ CRUD(์ƒ์„ฑ, ์กฐํšŒ, ์ˆ˜์ •, ์‚ญ์ œ) ๊ธฐ๋Šฅ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ํŽ˜์ด์ง•, ์ •๋ ฌ ๋“ฑ์„ ์‰ฝ๊ฒŒ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

3๏ธโƒฃ Spring Data JPA์˜ ์–ด๋…ธํ…Œ์ด์…˜

  • @Entity: ํด๋ž˜์Šค๋ฅผ JPA ์—”ํ‹ฐํ‹ฐ(Entity)๋กœ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค.
  • @Id: ๊ธฐ๋ณธ ํ‚ค(Primary Key)๋ฅผ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค.
  • @Repository: ๋ฐ์ดํ„ฐ ์ ‘๊ทผ ๊ฐ์ฒด(DAO)๋กœ ์‚ฌ์šฉ๋  ์ธํ„ฐํŽ˜์ด์Šค ๋˜๋Š” ํด๋ž˜์Šค์— ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. Spring์—์„œ ํ•ด๋‹น ํด๋ž˜์Šค๋ฅผ ๋นˆ์œผ๋กœ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ฉ๋‹ˆ๋‹ค.
  • @Query: JPQL ๋˜๋Š” ๋„ค์ดํ‹ฐ๋ธŒ ์ฟผ๋ฆฌ๋ฅผ ์ง์ ‘ ์ž‘์„ฑํ•  ๋•Œ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

๐Ÿ™‹โ€โ™‚๏ธ API์—์„œ์˜ ์ธํ„ฐํŽ˜์ด์Šค์™€ ์†Œํ”„ํŠธ์›จ์–ด ๊ณตํ•™์—์„œ์˜ ์ธํ„ฐํŽ˜์ด์Šค ๊ฐœ๋….
๐Ÿ™‹โ€โ™‚๏ธ JPA ์–ด๋…ธํ…Œ์ด์…˜ - @Entity
๐Ÿ™‹โ€โ™‚๏ธ JPA ์–ด๋…ธํ…Œ์ด์…˜ - @Id
๐Ÿ™‹โ€โ™‚๏ธ ์–ธ์ œ @Service,@Repository,@Controller์™€ ๊ฐ™์€ ์–ด๋…ธํ…Œ์ด์…˜์„ ์‚ฌ์šฉํ• ๊นŒ?
๐Ÿ™‹โ€โ™‚๏ธ ์—”ํ‹ฐํ‹ฐ(Entity)๋Š” ๋ฌด์—‡์ผ๊นŒ์š”?

3๏ธโƒฃ Spring Data JPA ์‚ฌ์šฉ ์˜ˆ์‹œ.

1๏ธโƒฃ ์—”ํ‹ฐํ‹ฐ ํด๋ž˜์Šค ์ •์˜.

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.GeneretedValue;
import javax.persistence.GenerationType;

@Entity
public class User {
    @Id
    @GeneretedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String name;
    private String email;
    
    // ๊ธฐ๋ณธ ์ƒ์„ฑ์ž, getter, setter ์ƒ๋žต
}

2๏ธโƒฃ Repository ์ธํ„ฐํŽ˜์ด์Šค ์ •์˜.

import org.springframework.data.repository.JpaRepository;

public interface UserRepository extends JpaRepository<User, Long> {
    // ๊ธฐ๋ณธ CRUD ๋ฉ”์„œ๋“œ ์™ธ์— ์ถ”๊ฐ€ ๋ฉ”์„œ๋“œ ์ •์˜
    User findByName(String name);
}
  • ์œ„ ์ฝ”๋“œ์—์„œ UserRepository๋Š” JpaRepository๋ฅผ ํ™•์žฅํ•˜์—ฌ, User ์—”ํ‹ฐํ‹ฐ(Entity)์™€ Long ํƒ€์ž…์˜ ๊ธฐ๋ณธ ํ‚ค(Primary Key)๋ฅผ ์‚ฌ์šฉํ•˜๋Š” Repository๋กœ ์ •์˜๋ฉ๋‹ˆ๋‹ค.
  • findByName: ๋ฉ”์„œ๋“œ ์ด๋ฆ„์„ ๊ธฐ๋ฐ˜์œผ๋กœ Spring Data JPA๊ฐ€ name ํ•„๋“œ๋กœ User๋ฅผ ์ฐพ๋Š” ์ฟผ๋ฆฌ๋ฅผ ์ž๋™์œผ๋กœ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.

3๏ธโƒฃ Service ํด๋ž˜์Šค์—์„œ ์‚ฌ์šฉ.

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class UserService {
    @Autowired
    private UserRepository userRepository;
    
    public List<User> getAllUsers() {
        return userRepository.findAll();
    }
    
    public User getUserByName(String name) {
        return userRepository.findByName(name);
    }
    
    public User createUser(User user) {
        return userRepository.save(user);
    }
}

4๏ธโƒฃ Spring Boot ์„ค์ •(application.properties)

spring.datasource.url=jdbc:mysql://localhost:3306/mydatabase
spring.datasource.username=root
spring.datasource.password=secret
spring.jpa.hibernate.ddl-auto=update

4๏ธโƒฃ Spring Data JPA์˜ ์žฅ์ .

1๏ธโƒฃ ๊ฐœ๋ฐœ ์ƒ์‚ฐ์„ฑ ํ–ฅ์ƒ.

  • ๋ฐ์ดํ„ฐ ์ ‘๊ทผ ์ฝ”๋“œ๋ฅผ ์ค„์—ฌ์ฃผ๋ฉฐ, ๋ฉ”์„œ๋“œ ์ด๋ฆ„ ๊ธฐ๋ฐ˜์˜ ์ฟผ๋ฆฌ ์ƒ์„ฑ์œผ๋กœ ๊ฐœ๋ฐœ ์†๋„๋ฅผ ๋†’์ž…๋‹ˆ๋‹ค.

2๏ธโƒฃ ์œ ์—ฐ์„ฑ.

  • ๋ณต์žกํ•œ ์ฟผ๋ฆฌ๋ฅผ ์ง์ ‘ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ๋‹ค์–‘ํ•œ ๊ธฐ๋Šฅ์„ ์‰ฝ๊ฒŒ ํ™•์žฅํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

3๏ธโƒฃ Spring ์ƒํƒœ๊ณ„์™€ ํ†ตํ•ฉ.

  • Spring Framework์™€ ์‰ฝ๊ฒŒ ํ†ตํ•ฉ๋˜๋ฉฐ, Spring Boot๋ฅผ ํ†ตํ•ด ์„ค์ •์ด ๊ฐ„ํŽธํ•ด์ง‘๋‹ˆ๋‹ค.

4๏ธโƒฃ ํŽ˜์ด์ง• ๋ฐ ์ •๋ ฌ ์ง€์›.

  • ๋Œ€๋Ÿ‰์˜ ๋ฐ์ดํ„ฐ๋ฅผ ํšจ์œจ์ ์œผ๋กœ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋„๋ก ๊ธฐ๋ณธ ํŽ˜์ด์ง•๊ณผ ์ •๋ ฌ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.

5๏ธโƒฃ ์š”์•ฝ.

  • Spring Data JPA๋Š” JPA ๊ธฐ๋ฐ˜์˜ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๊ฐœ๋ฐœ์„ ๋”์šฑ ์‰ฝ๊ฒŒ ํ•  ์ˆ˜ ์žˆ๋„๋ก ์ง€์›ํ•˜๋Š” Spring ๋ชจ๋“ˆ์ž…๋‹ˆ๋‹ค.
  • Repository ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ํ†ตํ•ด ๊ธฐ๋ณธ์ ์ธ CRUD(์ƒ์„ฑ, ์กฐํšŒ, ์ˆ˜์ •, ์‚ญ์ œ) ๊ธฐ๋Šฅ๊ณผ ๋ฉ”์„œ๋“œ ์ด๋ฆ„ ๊ธฐ๋ฐ˜์˜ ์ฟผ๋ฆฌ๋ฅผ ์ œ๊ณตํ•˜๋ฉฐ, ๊ฐœ๋ฐœ์ž๊ฐ€ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์™€์˜ ์ƒํ˜ธ์ž‘์šฉ ์ฝ”๋“œ๋ฅผ ์ค„์ผ ์ˆ˜ ์žˆ๋„๋ก ๋„์™€์ค๋‹ˆ๋‹ค.
  • ์ด๋ฅผ ํ†ตํ•ด ๋น ๋ฅด๊ณ  ๊ฐ„ํŽธํ•˜๊ฒŒ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ ‘๊ทผ ๊ณ„์ธต์„ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ๋” ๋ณต์žกํ•œ ์ฟผ๋ฆฌ๊ฐ€ ํ•„์š”ํ•œ ๊ฒฝ์šฐ ์ง์ ‘ ์ฟผ๋ฆฌ๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.