인프런 커뮤니티 질문&답변

이유경님의 프로필 이미지
이유경

작성한 질문수

자바 ORM 표준 JPA 프로그래밍 - 기본편

기본 키 매핑

@GeneratedValue 작성 후 오류

작성

·

678

0

[질문 템플릿]
1. 강의 내용과 관련된 질문인가요? (예)
2. 인프런의 질문 게시판과 자주 하는 질문에 없는 내용인가요? (예)
3. 질문 잘하기 메뉴얼을 읽어보셨나요? (예)

[질문 내용]
여기에 질문 내용을 남겨주세요.
 
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private String id;
위 코드 작성 후 오류가 납니다.. String 말고 숫자로 해도 오류가 나네요 ㅠ ..
mysql로 바꿨는데 왜 그런지 모르겠습니다ㅠ
 
오류 -WARN: GenerationTarget encountered exception accepting command : Error executing DDL " create table Member ( id varchar(255) not null auto_increment, name varchar(255) not null, primary key (id) ) engine=MyISAM" via JDBC Statement org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL " create table Member ( id varchar(255) not null auto_increment, name varchar(255) not null, primary key (id) ) engine=MyISAM" 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:14) Caused by: org.h2.jdbc.JdbcSQLSyntaxErrorException: Syntax error in SQL statement "\000d\000a create table Member (\000d\000a id varchar(255) not null auto_increment,\000d\000a name varchar(255) not null,\000d\000a primary key (id)\000d\000a ) engine[*]=MyISAM"; expected "identifier"; SQL statement: create table Member ( id varchar(255) not null auto_increment, name varchar(255) not null, primary key (id) ) engine=MyISAM [42001-210] at org.h2.message.DbException.getJdbcSQLException(DbException.java:521) at org.h2.message.DbException.getJdbcSQLException(DbException.java:496) at org.h2.message.DbException.getSyntaxError(DbException.java:265) at org.h2.command.Parser.readIdentifier(Parser.java:5759) at org.h2.command.Parser.parseCreateTable(Parser.java:9306) at org.h2.command.Parser.parseCreate(Parser.java:6779) at org.h2.command.Parser.parsePrepared(Parser.java:746) at org.h2.command.Parser.parse(Parser.java:674) at org.h2.command.Parser.parse(Parser.java:644) at org.h2.command.Parser.prepareCommand(Parser.java:551) at org.h2.engine.SessionLocal.prepareLocal(SessionLocal.java:615) at org.h2.server.TcpServerThread.process(TcpServerThread.java:288) 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:451) at org.h2.engine.SessionRemote.done(SessionRemote.java:607) at org.h2.command.CommandRemote.prepare(CommandRemote.java:85) at org.h2.command.CommandRemote.<init>(CommandRemote.java:51) at org.h2.engine.SessionRemote.prepareCommand(SessionRemote.java:477) at org.h2.jdbc.JdbcConnection.prepareCommand(JdbcConnection.java:1292) at org.h2.jdbc.JdbcStatement.executeInternal(JdbcStatement.java:217) at org.h2.jdbc.JdbcStatement.execute(JdbcStatement.java:205) at org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase.accept(GenerationTargetToDatabase.java:54) ... 15 more

 

오류
 
 
 
 
member
package hellojpa;

import javax.persistence.*;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;

@Entity
public class Member {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private String id;

@Column(name = "name", nullable = false)
private String username;

public Member(){

}

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}
}
 
 
 
jpaMain
package hellojpa;

import org.hibernate.internal.build.AllowSysOut;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import java.util.List;

public class JpaMain {

public static void main(String[] args) {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("hello");

EntityManager em = emf.createEntityManager();

EntityTransaction tx = em.getTransaction();
tx.begin();

try{

Member member = new Member();
//member.setId("ID_A");
member.setUsername("C");

em.persist(member);

tx.commit();

}catch (Exception e){
tx.rollback();
}finally {
em.close();
}
emf.close();



}

}








persistence.xml
<?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="org.h2.Driver"/>
<property name="javax.persistence.jdbc.user" value="sa"/>
<property name="javax.persistence.jdbc.password" value=""/>
<property name="javax.persistence.jdbc.url" value="jdbc:h2:tcp://localhost/~/test"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>

<!-- 옵션 -->
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
<property name="hibernate.use_sql_comments" value="true"/>
<property name="hibernate.hbm2ddl.auto" value="create" />
</properties>
</persistence-unit>
</persistence>

답변 1

1

안녕하세요. 이유경님, 공식 서포터즈 OMG입니다.

아래 영한님의 설명을 참고해주세요. 해결이 안될 경우 댓글남겨주세요.

https://www.inflearn.com/questions/204368


감사합니다.

이유경님의 프로필 이미지
이유경
질문자

이 방법으로 하니 터미널창에 오류는 안뜹니다.

근데 h2 콘솔 접속 JDBC URL 가 뭔지 모르겠는데 뭔가요?ㅠㅠ

DB와 연결하기 위한 정보라고 보시면 됩니다.

이유경님의 프로필 이미지
이유경
질문자

<!-- 필수 속성 -->
<property name="javax.persistence.jdbc.driver" value="org.h2.Driver"/>
<property name="javax.persistence.jdbc.user" value="sa"/>
<property name="javax.persistence.jdbc.password" value=""/>
<property name="javax.persistence.jdbc.url" value="jdbc:h2:mem://localhost/~/test;MODE=MYSQL"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL57Dialect"/>


이게 persistence.xml일부이고



이게 H2 console 인데 연결해서 들어가면 생성했던 member 테이블이 보이지 않습니다.
이렇게 연결하는거 맞을까요?

왜 바꾸신거죠?

<property name="javax.persistence.jdbc.url" value="jdbc:h2:mem://localhost/~/test;MODE=MYSQL"/>
<property name="javax.persistence.jdbc.url" value="jdbc:h2:tcp://localhost/~/test"/>

이 정보가 h2 JDBC URL에 입력되어야 하는 데이터입니다.

이유경님의 프로필 이미지
이유경

작성한 질문수

질문하기