[Spring boot + JPA Hibernate ] Postgres 연동하기

나르·2021년 10월 14일
1

Spring

목록 보기
1/24
post-thumbnail

📖 SETTING

pom.xml

<!-- DataBase -->
<dependency>
    <groupId>org.postgresql</groupId>
    <artifactId>postgresql</artifactId>
    <scope>runtime</scope>
</dependency>
<!-- JPA -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
    <version>2.5.4</version>
</dependency>

application.properties

# postgres db setting
spring.datasource.url=jdbc:postgresql://localhost:5432/{DBname}
spring.datasource.username={username}
spring.datasource.password={password}
spring.datasource.show-sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect

# hibernate setting
spring.jpa.properties.hibernate.use_sql_comments=true
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.properties.hibernate.show_sql=true
spring.jpa.hibernate.ddl-auto=create
spring.jpa.generate-ddl=true

📌 DB 별 url setting

Mysql
spring.datasource.url=jdbc:mysql://localhost:3306/testdb
Oracle
spring.datasource.url=jdbc:oracle:thin:@localhost:1521:xe
SQL Server
spring.datasource.url=jdbc:sqlserver://localhost:1433;databaseName=testdb
Postgres
spring.datasource.url=jdbc:postgresql://localhost:5432/testdb
MongoDB
spring.data.mongodb.uri=mongodb://root:rootpassword@localhost:27017/testdb
H2
jdbc:h2:~/testdb
Derby
jdbc:derby:testdb

📖 ERROR

Driver org.~.Driver claims to not accept jdbcUrl

application.properties 의 DB url이 잘못 됐을 경우 발생하는 에러이다.
spring.datasource.url=jdbc:postgresql://localhost:5432/{DBname}
나의 경우는 db명에 오타가 있었다.. 정확한 url로 수정해주면 해결.

error executing ddl via jdbc statement

DB 연결 후 테스트 하는 도중 가장 많이 발생한 에러이다.
이유로는 크게 세 가지를 찾았다.

  1. 쿼리 내(Column명 등)에 예약어가 포함되어있는 경우
  2. 테이블 명이 예약어인 경우
  3. alter 문법에 맞지 않는 쿼리가 실행된 경우

1, 2번의 경우는

  • @Column(name=""), @Table(name="") 을 이용해 예약어가 아닌 이름으로 변경해주거나
  • application.properties에
    spring.jpa.properties.hibernate.globally_quoted_identifiers=true
    를 추가하면 더블쿼트로 감싸져 컬럼과 테이블명으로 사용이 가능하다. (하지만 예약어를 쓰지 않는 편을 권장)

📌 DB 별 예약어

3번의 경우는
초기 설정 후 create 옵션으로 실행했을 때, table을 모두 drop하는데 alter할 테이블을 찾지 못해 나타났다.
spring.jpa.hibernate.ddl-auto=update
따라서 update로 옵션을 바꾼 뒤 실행하니 정상작동했다.

또한 엔티티 설정 중 @Column 어노테이션을 이용해서 컬럼 타입이나 기타 정보를 입력하지 않은 상태에서 실행할 경우에도 alert table 문장이 문법에 맞지 않아 위 오류가 발생한다. 동일하게 적절한 옵션으로 변경해서 처리하면 해결된다.

📌 Spring JPA / Hibernate 초기화 전략

Spring JPA / Hibernate의 DDL 옵션은 다음과 같다.

  • create : 기존 테이블을 삭제 후 새로 생성, DROP+CREATE
  • create-drop : 애플리케이션 종료시 생성한 DDL을 제거, DROP+CREATE+DROP
  • update : DB 테이블과 엔티티 매핑정보를 비교해 변경 사항만 수정
  • validate : DB 테이블과 엔티티 매핑정보를 비교해서 차이가 있으면 경고를 남기고 애플리케이션 실행X DDL 수정X
  • none : 자동생성 기능을 사용하지 않으려면 속성을 제거하거나, 유효하지 않은 옵션값을 주면 됨

따라서 DDL을 수정하는 옵션(create, create-drop,update)는 개발서버나 개발단계에서만 사용하고, 운영서버에서 사용하면 안된다.

  • 개발 초기 단계 : create, update
  • 테스트 서버 : update, validate
  • 스테이징, 운영 서버 : validate, none

Reference

https://www.yawintutor.com/runtimeexception-driver-claims-to-not-accept-jdbcurl/
https://yjh5369.tistory.com/entry/Spring-boot-JPA%EC%97%90%EC%84%9C-DB-%EC%98%88%EC%95%BD%EC%96%B4%EB%A5%BC-column%EC%9C%BC%EB%A1%9C-%EC%82%AC%EC%9A%A9%ED%95%98%EB%8A%94-%EB%B0%A9%EB%B2%95
https://doublesprogramming.tistory.com/260
https://galid1.tistory.com/610

profile
💻 + ☕ = </>

0개의 댓글