BE전문가 프로젝트

JPA 프로젝트 생성(HelloWolrd) 후 DB 설정 본문

JPA

JPA 프로젝트 생성(HelloWolrd) 후 DB 설정

원호보고서 2022. 9. 5. 23:10

 

1. Hibernate 라이브러리 추가

- 하이버네이트를 추가해주는데 여기서 스프링 버전에 맞게 선택해주는 것이 가장 이상적이다.

- 스프링부트 홈페이지에 Reference를 가서 orf.hibernate를 ctrl+f 하여 찾아보면 지금 버전에 맞는 하이버네이트 버전을 알 수 있다.

라이브러리에서 보면 javax.persistence를 찾아볼 수 있다. JPA는 인터페이스인데 인터페이스 구현체로 하이버네이트를 선택했다는 것을 알 수 있다. 

1.1 could not find artifact org.springframework 오류

  • 스프링부트 프로젝트 생성하였는데 오류가 발생(위에 오류메시지)
  • 기존에 존재하던 Library 오류가 원인 가능성 높다.

해결방법

사용자/내폴더/.m2안에 있는 repository를 삭제 후 다시 pom.xml build한다.

 

 

 

2. JPA 설정하기 - persistence.xml(JPA 설정파일)

- 위치 : /META-INF/persistence.xml에 위치

- persistence-unit name으로 이름 지정

- javax.persistence로 시작 : JPA 표준 속성

- hibernate로 시작 : 하이버네이트 전용 속성

 

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.2"
             xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_2.xsd">

    <persistence-unit name="hello">
            <properties>
                <!-- 필수 속성 -->
                <property name="javax.persistence.jdbc.driver" value="com.mysql.cj.jdbc.Driver"/>
                <property name="javax.persistence.jdbc.user" value="root"/>
                <property name="javax.persistence.jdbc.password" value="1234"/>
                <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/?characterEncoding=UTF-8&amp;serverTimezone=UTC"/>

                <!-- 하이버네이트 사용 시 다른 DB에서 MySQL 문법을 사용 가능하도록 변경.-->
                <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL8Dialect"/>
                <!-- 콘솔에 SQL 출력 여부 -->
                <property name="hibernate.show_sql" value="true"/>
                <!-- 가독성 높여주는 formatting 여부 -->
                <property name="hibernate.format_sql" value="true"/>
                <!-- Comment 확인 여부 -->
                <property name="hibernate.use_sql_comments" value="true"/>
            </properties>
    </persistence-unit>
</persistence>

 

 

Comments