02. JPA(Hibernate) - YAPP-16th/Team_Web_3_Backend GitHub Wiki

ORM(Object-relational mapping) ์ด๋ž€?

  • Object-relational mapping (๊ฐ์ฒด ๊ด€๊ณ„ ๋งคํ•‘)
  • ๊ฐ์ฒด๋Š” ๊ฐ์ฒด๋Œ€๋กœ ์„ค๊ณ„ํ•˜๊ณ , ๊ด€๊ณ„ํ˜• ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋Š” ๊ด€๊ณ„ํ˜• ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋Œ€๋กœ ์„ค๊ณ„ํ•œ๋‹ค.
  • ORM ํ”„๋ ˆ์ž„์›Œํฌ๊ฐ€ ์ค‘๊ฐ„์—์„œ ๋งคํ•‘ํ•ด์ค€๋‹ค.

JPA(Java Persistence API) ์ด๋ž€?

์ž๋ฐ” ORM ๊ธฐ์ˆ  ํ‘œ์ค€์œผ๋กœ, ์ธํ„ฐํŽ˜์ด์Šค์˜ ๋ชจ์Œ์ด๋‹ค. JPA ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•œ ๋Œ€ํ‘œ์ ์ธ ์˜คํ”ˆ์†Œ์Šค๊ฐ€ Hibernate

์ƒ์‚ฐ์„ฑ

  • ๊ฐœ๋ฐœ์ž๊ฐ€ ์ผ์ผํžˆ CRUD์šฉ ์ฟผ๋ฆฌ๋ฅผ ์ž‘์„ฑํ•ด์ค˜์•ผํ•˜๋˜ Mybatis์™€ ๊ฐ™์€ Mapper๋ฐฉ์‹์€ ์ปฌ๋Ÿผ์ด ์ถ”๊ฐ€๋˜๊ฑฐ๋‚˜ํ•˜๋ฉด ์ˆ˜์ •ํ•ด์ฃผ์–ด์•ผํ•˜๋Š” ๋ถ€๋ถ„์ด ์ƒ๋‹นํžˆ ๋งŽ์•˜๋‹ค. ์ด๋กœ ์ธํ•ด์„œ ์ž๋ฐ”๋ฅผ ์‚ฌ์šฉํ•˜์ง€๋งŒ ๊ฐ์ฒด์ค‘์‹ฌ ๊ฐœ๋ฐœ์ด ์•„๋‹ˆ๋ผ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ํ๋ฆ„์œผ๋กœ ๊ฐœ๋ฐœ์„ ํ•˜๊ฒŒ๋˜๋Š” ๋ฌธ์ œ๊ฐ€ ์žˆ๋‹ค. JPA๋ฅผ ์‚ฌ์šฉํ•˜๊ฒŒ ๋˜๋ฉด ์ฟผ๋ฆฌ๋ฅผ ์ง์  ์ƒ์„ฑํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๊ณ  ๋งŒ๋“ค์–ด์ง„ ๊ฐ์ฒด๋กœ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ๋‹ค๋ฃจ๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ์ฒด ์ค‘์‹ฌ์œผ๋กœ ๊ฐœ๋ฐœ์„ ์ง„ํ–‰

์œ ์ง€๋ณด์ˆ˜

  • SQL์„ ์ง์ ‘์ ์œผ๋กœ ์ž‘์„ฑํ•˜์ง€ ์•Š๊ณ  ์—”ํ‹ฐํ‹ฐ ํ•„๋“œ๊ฐ€ ๋˜๋Š” ๊ฐ์ฒด๋ฅผ ๋‹ค๋ค„์„œ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ๋™์ž‘์‹œํ‚ค๊ธฐ ๋•Œ๋ฌธ์— ์œ ์ง€๋ณด์ˆ˜๊ฐ€ ๋”์šฑ ๊ฐ„๊ฒฐํ•˜๋‹ค. ์™œ๋ƒํ•˜๋ฉด ์ฟผ๋ฆฌ๊ฐ€ ์ˆ˜์ •๋˜๋ฉด ๊ทธ์— ๋”ฐ๋ผ์„œ ๊ทธ๋ฅผ ๋‹ด์„ DTO ํ•„๋“œ๋„ ๋ชจ๋‘ ๋ณ€๊ฒฝ์ด ๋˜์•ผ ํ•˜์ง€๋งŒ JPA๋ฅผ ์‚ฌ์šฉํ•˜๊ฒŒ ๋˜๋ฉด ๋‹จ์ˆœํžˆ ์—”ํ‹ฐํ‹ฐ ํด๋ž˜์Šค ์ •๋ณด๋งŒ ๋ณ€๊ฒฝํ•˜๋ฉด ์‰ฝ๊ฒŒ ๊ด€๋ฆฌ๊ฐ€ ๊ฐ€๋Šฅ

JPA ๊ธฐ๋ณธ์„ค์ •

build.gradle

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.1.10.RELEASE")
    }
}

apply plugin: 'java'
apply plugin: 'spring-boot'

sourceCompatibility = 1.5
version = '1.0'

repositories {
    mavenCentral()
}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.11'
    compile("org.springframework.boot:spring-boot-starter-web:1.2.0.RELEASE")
    compile("org.springframework.boot:spring-boot-starter-data-jpa")
    compile("mysql:mysql-connector-java:5.1.34")
}

dependencies๋Š” ์Šคํ”„๋ง ๋ถ€ํŠธ๋ฅผ ์“ฐ๋ ค๋ฉด spring-boot-starter-web์ด ์žˆ์–ด์•ผ ํ•˜๊ณ , ๋ฐ์ดํ„ฐ์—ฐ๋™ํ•˜๋Š” ๊ฒƒ์„ ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” spring-boot-starter-data-jpa๊ฐ€ ์žˆ์–ด์•ผ ํ•˜๊ณ , mysql์„ ์“ฐ๋ ค๋ฉด mysql-connector-java๋ฅผ ์ถ”๊ฐ€ ํ•„์š”(JPA์— ํ•˜์ด๋ฒ„๋„ค์ดํŠธ ๋‚ด์žฅ)

main class

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(Application.class, args);

    }
}

@SpringBootApplication์–ด๋…ธํ…Œ์ด์…˜์„ ๋ถ™์ด๋ฉด ์ตœ์ดˆ ๊ธฐ๋ณธ์…‹ํŒ…์œผ๋กœ ํ†ฐ์บฃ์„ ๋„์›Œ์„œ 8080ํฌํŠธ๋กœ ์„œ๋ฒ„ ์ƒ์„ฑ

application.properties ๋ชจ๋ธ ์ƒ์„ฑ

spring.datasource.url=jdbc:mysql://127.0.0.1/sosi?autoReconnect=true&useUnicode=true&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.database=mysql
spring.jpa.show-sql=true

์†์„ฑ๊ฐ’ ddl-auto๋ถ€๋ถ„์ด create-drop์€ ์„œ๋ฒ„ ์žฌ์‹œ์ž‘ ๋•Œ๋งˆ๋‹ค ํ…Œ์ด๋ธ”์„ ๋‚ ๋ ค๋ฒ„๋ฆฌ๋Š” ์˜ต์…˜์ž…๋‹ˆ๋‹ค.(ํ…Œ์ŠคํŠธ ์˜ต์…˜)

@Entity
public class TEST {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;

    @Column(nullable = false)
    private String name;

    @OneToMany
    @JoinColumn(name="TEST_id", referencedColumnName="id")
    private List<testTable> testTableList;

    public TEST() {
    }

    public TEST(String name) {
        this.name = name;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<testTable> getTestTableList() {
        return testTableList;
    }

    public void setTestTableList()(List<Schedule> testTableList()) {
        this.testTableList = testTableList;
    }

}
@Entity
public class TEST_01 {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @ManyToOne
    @JsonBackReference
    private Test test;

    @Column
    private String program;

    public TEST_01() {
    }

    public TEST_01(TEST test, String content) {
        this.test = test;
        this.content = content;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Test getTest() {
        return test;
    }

    public void setTest(Test test) {
        this.test = test;
    }

    public String getProgram() {
        return program;
    }

    public void setProgram(String program) {
        this.program = program;
    }

}

๋‘ ๊ฐœ์˜ ๋ชจ๋ธ์ด 1:N๊ด€๊ณ„ ์ด๋ฉฐ TEST๊ฐ€ ์Šค์ผ€์ค„์„ ์—ฌ๋Ÿฌ๊ฐœ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋Š” ๊ตฌ์กฐ์ž…๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ Test_01์—๋Š” ManyToOne์˜ต์…˜ @JsonBackReference ๊ธฐ์กดDao์—์„œ๋Š” ํ•˜์ด๋ฒ„๋„ค์ดํŠธ์„ธ์…˜์„ ํ†ตํ•ด CRUD ์ œ๊ณตํ•˜์˜€์œผ๋ฉฐ, Spring Data JPA์—์„œ๋Š” JpaRepository๋ฅผ ์ œ๊ณตํ•˜๋ฉด์„œ CRUD ์ œ๊ณต

๋ ˆํŒŒ์ง€ํ† ๋ฆฌ ์ƒ์„ฑ

TestRepository.java
public interface TestRepository extends JpaRepository<Test, Long>{
}
Test_01Repository.java

public interface Test_01Repository extends JpaRepository<Test_01, Long> {
}

์ปจํŠธ๋กค๋Ÿฌ

TestController.java

@RestController
@RequestMapping("/test")
public class TestController {

    @Autowired
    private TestRepository testRepository;

    @RequestMapping("{testId}")
    public Test getTest(@PathVariable Long testId) {
        Test test = Test_01Repository.findOne(tesId);
        return test;
    }
}

Test_01Controller.java

@RestController
@RequestMapping("/schedule")
public class Test_01Controller {

    @Autowired
    private Test01Repository scheduleRepository;

    @Autowired
    private Test_01Repository test_01Repository;

    @RequestMapping("{test_01Id}")
    public Schedule getSchedule(@PathVariable Long Test_01Id) {
        Test_01 test_01 = Test_01Repository.findOne(Test_01Id);
        Test test = schedule.getTest();
        return schedule;
    }

    @RequestMapping("add/{TestId}")
    public Schedule addSchedule(@PathVariable Long TestId, @RequestParam(value="program") String program) {
        Test test = TestRepository.findOne(testId);
        Test test = testRepository.save(new Schedule(test, program));

        return test;
    }
}

๊ฐ’ ์ƒ์„ฑ


@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(Application.class, args);
        TestRepository testRepository = context.getBean(TestRepository.class);

        testRepository.save(new Test("๊ณผ๋…„"));
        testRepository.save(new Test("์„ ์•„"));
 
    }
}

JPA ๊ธฐ๋ณธ ๋กœ์ง ๋ฐ Setting

โš ๏ธ **GitHub.com Fallback** โš ๏ธ