BE전문가 프로젝트
JPA 프로젝트 생성(HelloWolrd) 후 DB 설정 본문
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&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>
'JPA' 카테고리의 다른 글
영속성 관리(JPA 내부 구조) (0) | 2022.09.25 |
---|---|
JPQ의 작동원리와 JPA 애플리케이션 개발 및 JPQL 맛보기 (0) | 2022.09.18 |
JPA란? (0) | 2022.08.14 |
JPA 탄생배경 (0) | 2022.08.03 |
1.3 Products Service 설계 - Products(ProductService.interface) (0) | 2022.05.12 |
Comments