org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL " @GeneratedValue() 테이블 생성 이슈 및 Insert 쿼리 안나가는 이슈 해결하기
문제상황
환경
1. JPA 5.3.10 Final
2. h2 1.4.199
3. 자바 11
4. <property name="hibernate.hbm2ddl.auto" value="create"/>
@Entity
public class Member {
@Id
//**
//이부분이 오류의 원인이다
//**
@GeneratedValue(strategy = GenerationType.IDENTITY)
private String id;
@Column(name = "name", nullable = false)
private String username;
public Member() {
}
public Member(String id, String username) {
this.id = id;
this.username = username;
}
/**
* getter setter
*/
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
JPA 실습 중
@GeneratedValue(strategy = GenerationType.IDENTITY)
위의 코드를 추가하자 테이블을 생성하지 못하는 에러가 발생하였다.
에러코드
WARN: GenerationTarget encountered exception accepting command : Error executing DDL "
create table Member (
id varchar(255) generated by default as identity,
name varchar(255) not null,
primary key (id)
)" via JDBC Statement
org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL "
create table Member (
id varchar(255) generated by default as identity,
name varchar(255) not null,
primary key (id)
)" via JDBC Statement
at org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase.accept(GenerationTargetToDatabase.java:67)
at org.hibernate.tool.schema.internal.SchemaCreatorImpl.applySqlString(SchemaCreatorImpl.java:440)
at org.hibernate.tool.schema.internal.SchemaCreatorImpl.applySqlStrings(SchemaCreatorImpl.java:424)
at org.hibernate.tool.schema.internal.SchemaCreatorImpl.createFromMetadata(SchemaCreatorImpl.java:315)
at org.hibernate.tool.schema.internal.SchemaCreatorImpl.performCreation(SchemaCreatorImpl.java:166)
at org.hibernate.tool.schema.internal.SchemaCreatorImpl.doCreation(SchemaCreatorImpl.java:135)
at org.hibernate.tool.schema.internal.SchemaCreatorImpl.doCreation(SchemaCreatorImpl.java:121)
at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.performDatabaseAction(SchemaManagementToolCoordinator.java:155)
at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.process(SchemaManagementToolCoordinator.java:72)
at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:310)
at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:467)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:939)
at org.hibernate.jpa.HibernatePersistenceProvider.createEntityManagerFactory(HibernatePersistenceProvider.java:56)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:79)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:54)
at hellojpa.JpaMain.main(JpaMain.java:10)
Caused by: org.h2.jdbc.JdbcSQLFeatureNotSupportedException: Feature not supported: "CHARACTER VARYING(255)"; SQL statement:
create table Member (
id varchar(255) generated by default as identity,
name varchar(255) not null,
[50100-210]
at org.h2.message.DbException.getJdbcSQLException(DbException.java:575)
at org.h2.message.DbException.getJdbcSQLException(DbException.java:496)
at org.h2.message.DbException.get(DbException.java:227)
at org.h2.message.DbException.get(DbException.java:203)
at org.h2.message.DbException.getUnsupportedException(DbException.java:291)
at org.h2.command.ddl.SequenceOptions.getBounds(SequenceOptions.java:316)
at org.h2.command.ddl.SequenceOptions.getBounds(SequenceOptions.java:244)
at org.h2.schema.Sequence.<init>(Sequence.java:101)
at org.h2.table.Column.initializeSequence(Column.java:459)
at org.h2.command.ddl.CommandWithColumns.generateSequences(CommandWithColumns.java:103)
at org.h2.command.ddl.CreateTable.update(CreateTable.java:110)
at org.h2.command.CommandContainer.update(CommandContainer.java:174)
at org.h2.command.Command.executeUpdate(Command.java:252)
at org.h2.server.TcpServerThread.process(TcpServerThread.java:408)
at org.h2.server.TcpServerThread.run(TcpServerThread.java:191)
at java.base/java.lang.Thread.run(Thread.java:834)
at org.h2.message.DbException.getJdbcSQLException(DbException.java:507)
at org.h2.engine.SessionRemote.done(SessionRemote.java:611)
at org.h2.command.CommandRemote.executeUpdate(CommandRemote.java:237)
at org.h2.jdbc.JdbcStatement.executeInternal(JdbcStatement.java:228)
at org.h2.jdbc.JdbcStatement.execute(JdbcStatement.java:201)
at org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase.accept(GenerationTargetToDatabase.java:54)
... 15 more
해결
Long 타입으로 바꾸어주자 해결이 되었다.
추가이슈
그러나
ERROR: NULL not allowed for column "ID"; SQL statement:
위와 같은 에러가 발생하며 데이터가 들어가지 않았다. @GeneratedValue를 사용해 자동으로 Id를 생성하므로, setId를 통해 값을 넣는 코드를 일부러 넣지 않았는데, 이와 관련해 문제가 발생한듯 하다.
추가 이슈 해결 방법
1. GeneratedValue의 타입을 AUTO로 바꾸니 해결되었다.
2. GenratedValue 타입을 그대로 두고 jdbc에 MODE=LEGACY 코드를 추가해 해결하였다.
<property name="javax.persistence.jdbc.url" value="jdbc:h2:tcp://localhost/~/test;MODE=LEGACY"/>
3. h2버전을 1.4.200 -> 2.0.204로 올린다. ( hibernate 와 h2의 버전문제로 인한 버그가 많은 듯)
원인
stackoverflow에서는
1.4.200버전에서 취약성이 있어서 발생하는 문제들이라고 한다. 2.0.204 로 버전을 올려보라고 해서 올리고 해보았는데 잘 동작한다.
<!-- H2 데이터베이스 -->
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>2.0.204</version>
</dependency>
참고
'IT > 디버깅' 카테고리의 다른 글
[Spring] how to get selected option value by requestParam in spring thymeleaf (0) | 2022.09.02 |
---|---|
Failed to load ApplicationContext 에러 해결 (22) | 2022.08.23 |
스프링 @ModelAttribute 데이터 매핑안됨 (0) | 2022.07.29 |
java: incompatible types capture#1 of ? extends cannot be converted to 원인 및 해결 wildcard (0) | 2022.07.15 |
[Android] 카카오링크 앱 연결시 앱실행 안되는 문제 해결하기 (0) | 2022.06.01 |