뭐든 즐기면서 ;)

Spring Boot(Gradle) + MySQL + JPA(Hibernate) [2] - Spring Data JPA 설정 1 본문

BACK/Spring Boot & JPA

Spring Boot(Gradle) + MySQL + JPA(Hibernate) [2] - Spring Data JPA 설정 1

Tada.*+ 2021. 10. 25. 19:35
728x90

참고 : https://velog.io/@chyin370/ToyProject-JPAHibernate-%EC%A0%81%EC%9A%A9%ED%95%98%EA%B8%B0

1. application.properties

spring.datasource.url=jdbc:mysql://localhost:3306/study_db?autoReconnect=true

spring.datasource.username=root

spring.datasource.password=data~secret!

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

spring.jpa.database=mysql

spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect

spring.jpa.open-in-view=false

spring.jpa.show-sql=true

spring.jpa.generate-ddl=true

spring.jpa.hibernate.ddl-auto=create

spring.jpa.properties.hibernate.format_sql=true

logging.level.web=DEBUG

logging.level.org.hibernate.SQL=DEBUG

logging.level.org.hibernate.type.descriptor.BasicBinder=TRACE

# dummy data

spring.jpa.properties.hibernate.hbm2ddl.import_files=classpath:sql/init-import.sql

spring.jpa.properties.hibernate.hbm2ddl.import_files_sql_extractor=org.hibernate.tool.hbm2ddl.MultipleLinesSqlCommandExtractor

2. Entity 생성

@Enumerated 어노테이션 부가 설명 참고 : https://lng1982.tistory.com/280

* error 발생 : org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: Attribute [com.study.jpa.entity.User.gender] was annotated as enumerated, but its java type is not an enum [java.lang.String]

해결 방법 ↓ ↓ ↓ ↓

Enum class로 생성해 준다.

3. SQL 작성

application.properties에 작성한 spring.jpa.properties.hibernate.hbm2ddl.import_files 에 대응되는 sql파일

* error 발생 : org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL "INSERT INTO user (email, ....................

Caused by: java.sql.SQLSyntaxErrorException: Unknown column 'cellPhone' in 'field list'

해결 방법 : Entity에 카멜 표기법으로 선언한 변수 칼럼을 스네이크 표기법으로 변경하여 sql 작성해야 함.

* error 또 발생 : Caused by: java.sql.SQLException: Column count doesn't match value count at row 1

해결 방법 : insert 문에 칼럼과 value 작성 수가 같지 않음. (cell_phone value 입력 안 함..)

4. Spring Boot 실행

5. DB Insert 값 확인

728x90
Comments