[H2] Column이 drop 되지 않음 | JPA Bean을 직접 주입하는 실습 중
⭐ 설정파일을 잘못 작성 (create-drop의 위치)
문제 원인
: jpa 설정을 JpaProperties에서 가져왔기 때문에,
jpa의 create-drop 설정은 jpa의 properties 아래에 둬야 의도에 맞다.
∴ JpaProperties에서 설정을 가져올 때의 ddl auto 설정의 올바른 위치:
jpa:
properties:
hbm2ddl:
auto: create-drop
∨ create-drop이 jpa의 properties 아래로 가야 한다.
∨ JPA에 필요한 Bean을 직접 주입하는 실습이었다. 접은글의 코드를 보면, 위와 같은 jpa설정을 JpaProperties에서 가져오고 있다.
아래 코드를 보면 JpaProperties에서 정보를 가져옴을 알 수 있다.
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource, JpaVendorAdapter jpaVendorAdapter,
JpaProperties jpaProperties) {
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setDataSource(dataSource);
em.setPackagesToScan("com.example.demo.domain");
em.setJpaVendorAdapter(jpaVendorAdapter);
Properties properties = new Properties();
properties.putAll(jpaProperties.getProperties());
em.setJpaProperties(properties);
return em;
}
∨ 하지만 수정 전에는 jpa 바로 아래에 create-auto 설정을 뒀다.
그래서 created-auto 설정이 먹히지 않았던 것이다.
// 수정 전!
jpa:
generate-ddl: true
hibernate:
ddl-auto: create-drop
아래는 문제 해결 전 고민한 내용이다.
이유를 찾지 못해서 결국 임시방편으로 해결하고 넘겼던..
결론은 create-drop가 안 읽히는 위치에 있는게 문제였다.
drop 로그가 안 보였던 것이 당연하다.
🚨문제점
AGE컬럼은 이전 실습에서 만든 컬럼이다.
예상대로라면 AGE 컬럼은 없어야 한다.
- hibernate의 ddl-auto 설정이 create-drop이기 때문이다.
- 그리고 h2 db는 휘발성이라서 application을 재구동하면 초기화되어야 한다고 생각했다.
없어야 할 AGE 컬럼뿐만 아니라, 이전에 생성해둔 불필요한 컬럼도 drop되지 않는다.
(참고: 이전에 시험삼아 jpa Entity 컬럼명을 snack case로 변경하여 넣도록 했었음)
아래 컬럼에서 불필요한 컬럼은 AGE, FIRST_NAME, LAST_NAME의 3가지이다.
- 지금 Entity에 정의되어있지 않은 컬럼인데 왜 create-drop 설정이 안 먹힐까?
- 왜 create-drop 되지 않을까?
(로그에서 drop을 실패했다는 부분도 발견하지 못했음)

이유를 찾지 못했다.
일단 실습은 진행해야해서 저 컬럼을 손수 지워주었다.
🚨 직접 컬럼을 삭제해줌 ㅠㅡㅠ
그래서 '왜 컬럼이 drop되지 않지..'는 일단 미스테리로 두었다.
일단 H2 console에서 직접 drop했다.
실습을 이어 진행할 수 있도록 말이다.

서버를 재구동하여 컬럼을 정상화하고..
컬럼이 Entity대로 잘 등록되었다.

❗참고 : 문제해결의 발단
repository.save(entity)할 때 아래 에러가 발생했었다.
NULL not allowed for column "AGE"; SQL statement:
AGE는 예전 실습에서 만든 컬럼인데.. 왜 아직도 있는겨?
그래서 작성하게 된 글임!