작성
·
2.7K
4
섹션7의 고급 매핑 따라하면서 Exception이 발생 하여 질문 드립니다.
섹션7의 고급 매핑처럼, class로 Item, Book, Album, Movie를 만들었습니다.
Main Class에 아래 처럼 코딩 하여 돌려면 Movie 와 Item table에 값이 들어 갑니다.
Persistence.xml 에 설정이 <property name="hibernate.hbm2ddl.auto" value="create" /> 라서
매번 Debug로 Intellig 실행시 table이 drop 되고 다시 create 됩니다.
====== Persistence.xml ===========
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence" version="2.1">
<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="bistel01"/>
<property name="javax.persistence.jdbc.url" value=" jdbc:h2:tcp://localhost/~/h2database"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect" />
<!-- 옵션 -->
<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.id.new_generator_mappings" value="true" />
<property name="hibernate.hbm2ddl.auto" value="create" />
</properties>
</persistence-unit>
</persistence>
Intellig 에서 한번 수행은 정상적으로 되나 그후 두번째 debug 로 수행 하면
Table drop 할때 exceptin 이 납니다.
Exception을 보면 Item 과 Movie 에 이미 data가 들어가 있고, 두 table 간의 연관 관계가 FK(?) 로 잡혀 져 있는데
Table Drop을 sub table 즉 movie, book, album을 먼저 수행 하고 그 다음에 Item을 해야 하는데 drop 순서가
Albme, Book, Item , Movie 이다 보니 발생 하는 듯 합니다.
이럴때는 어떻게 Table의 연관 관계를 끊나요 .( 오라클은 이처럼 지우면 되는데==> DROP TABLE CASCADE CONSTRAINTS)
======EXCEPTION LOG======
INFO: HHH10001003: Autocommit mode: false
8월 18, 2020 2:47:18 오후 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl$PooledConnections <init>
INFO: HHH000115: Hibernate connection pool size: 20 (min=1)
8월 18, 2020 2:47:18 오후 org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.H2Dialect
8월 18, 2020 2:47:19 오후 org.hibernate.cfg.AnnotationBinder processJoinedDiscriminatorProperties
WARN: HHH000457: Joined inheritance hierarchy [hellojpa.shopmall.Item] defined explicit @DiscriminatorColumn. Legacy Hibernate behavior was to ignore the @DiscriminatorColumn. However, as part of issue HHH-6911 we now apply the explicit @DiscriminatorColumn. If you would prefer the legacy behavior, enable the `hibernate.discriminator.ignore_explicit_for_joined` setting (hibernate.discriminator.ignore_explicit_for_joined=true)
Hibernate:
drop table Album if exists
Hibernate:
drop table Book if exists
Hibernate:
drop table Item if exists
8월 18, 2020 2:47:20 오후 org.hibernate.resource.transaction.backend.jdbc.internal.DdlTransactionIsolatorNonJtaImpl getIsolatedConnection
INFO: HHH10001501: Connection obtained from JdbcConnectionAccess [org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator$ConnectionProviderJdbcConnectionAccess@317a118b] for (non-JTA) DDL execution was not in auto-commit mode; the Connection 'local transaction' will be committed and the Connection will be set into auto-commit mode.
8월 18, 2020 2:47:20 오후 org.hibernate.tool.schema.internal.ExceptionHandlerLoggedImpl handleException
WARN: GenerationTarget encountered exception accepting command : Error executing DDL "
drop table Item if exists" via JDBC Statement
org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL "
drop table Item if exists" via JDBC Statement
at org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase.accept(GenerationTargetToDatabase.java:67)
at org.hibernate.tool.schema.internal.SchemaDropperImpl.applySqlString(SchemaDropperImpl.java:375)
at org.hibernate.tool.schema.internal.SchemaDropperImpl.applySqlStrings(SchemaDropperImpl.java:359)
at org.hibernate.tool.schema.internal.SchemaDropperImpl.dropFromMetadata(SchemaDropperImpl.java:241)
at org.hibernate.tool.schema.internal.SchemaDropperImpl.performDrop(SchemaDropperImpl.java:154)
at org.hibernate.tool.schema.internal.SchemaDropperImpl.doDrop(SchemaDropperImpl.java:126)
at org.hibernate.tool.schema.internal.SchemaDropperImpl.doDrop(SchemaDropperImpl.java:112)
at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.performDatabaseAction(SchemaManagementToolCoordinator.java:144)
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:15)
Caused by: org.h2.jdbc.JdbcSQLSyntaxErrorException: Cannot drop "ITEM" because "FK5SQ6D5AGRC34ITHPDFS0UMO9G" depends on it; SQL statement:
drop table Item if exists [90107-200]
at org.h2.message.DbException.getJdbcSQLException(DbException.java:576)
at org.h2.message.DbException.getJdbcSQLException(DbException.java:429)
at org.h2.message.DbException.get(DbException.java:205)
at org.h2.command.ddl.DropTable.prepareDrop(DropTable.java:98)
at org.h2.command.ddl.DropTable.update(DropTable.java:124)
at org.h2.command.CommandContainer.update(CommandContainer.java:198)
at org.h2.command.Command.executeUpdate(Command.java:251)
at org.h2.server.TcpServerThread.process(TcpServerThread.java:406)
at org.h2.server.TcpServerThread.run(TcpServerThread.java:183)
at java.lang.Thread.run(Unknown Source)
at org.h2.message.DbException.getJdbcSQLException(DbException.java:576)
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
답변 1
19
안녕하세요. 조세희님^^
이게 h2 데이터베이스 1.4.200에서 발생하는 문제입니다.
해결방안은 다음과 같습니다.
해결방안1
h2 데이터베이스 버전을 1.4.199로 낮춘다.
해결방안2 (권장)
h2 데이터베이스 버전을 1.4.200을 유지하고, 하이버네이트 버전을 5.4.13.Final 버전 이상부터 사용한다.
하이버네이트 5.4.13.Final 부터 H2 데이터베이스 1.4.200을 제대로 지원합니다. drop 테이블 이슈도 해결되었습니다.
도움이 되셨길 바래요^^