Hibernate SchemaExport 无法首先创建架构
Posted
技术标签:
【中文标题】Hibernate SchemaExport 无法首先创建架构【英文标题】:Hibernate SchemaExport fails to first create schema 【发布时间】:2013-07-23 08:10:17 【问题描述】:我正在使用 Arquillian、JBoss、JPA/Hibernate、H2 DB 和 Maven 运行测试。在我的测试 persistence.xml 文件中,我有:
<property name="hibernate.hbm2ddl.auto" value="create-drop" />
<property name="hibernate.show_sql" value="true" />
现在我有一个通过 Hibernate 注释映射到“用户”表的单个用户类。
一切都差不多了。问题是 Hibernate 正在尝试执行:
drop table my_schema.users if exists
但是架构“my_schema”不存在,所以它失败了(毕竟我是针对内存数据库运行的)。
如何让休眠状态执行它似乎忘记的“创建模式 my_schema”步骤?
更新: 我从 Hibernate 看到的消息:
09:42:45,402 INFO [org.jboss.as.jpa] (MSC service thread 1-7) JBAS011402: Starting Persistence Unit Service 'test.war#ccmc'
09:42:45,524 INFO [org.hibernate.annotations.common.Version] (MSC service thread 1-7) HCANN000001: Hibernate Commons Annotations 4.0.1.Final
09:42:45,532 INFO [org.hibernate.Version] (MSC service thread 1-7) HHH000412: Hibernate Core 4.0.1.Final
09:42:45,535 INFO [org.hibernate.cfg.Environment] (MSC service thread 1-7) HHH000206: hibernate.properties not found
09:42:45,542 INFO [org.hibernate.cfg.Environment] (MSC service thread 1-7) HHH000021: Bytecode provider name : javassist
09:42:45,572 INFO [org.hibernate.ejb.Ejb3Configuration] (MSC service thread 1-7) HHH000204: Processing PersistenceUnitInfo [
name: ccmc
...]
09:42:45,739 INFO [org.hibernate.service.jdbc.connections.internal.ConnectionProviderInitiator] (MSC service thread 1-7) HHH000130: Instantiating explicit connection provider: org.hibernate.ejb.connection.InjectedDataSourceConnectionProvider
09:42:45,956 INFO [org.hibernate.dialect.Dialect] (MSC service thread 1-7) HHH000400: Using dialect: org.hibernate.dialect.H2Dialect
09:42:45,962 WARN [org.hibernate.dialect.H2Dialect] (MSC service thread 1-7) HHH000431: Unable to determine H2 database version, certain features may not work
09:42:45,965 INFO [org.hibernate.engine.jdbc.internal.LobCreatorBuilder] (MSC service thread 1-7) HHH000423: Disabling contextual LOB creation as JDBC driver reported JDBC version [3] less than 4
09:42:45,973 INFO [org.hibernate.engine.transaction.internal.TransactionFactoryInitiator] (MSC service thread 1-7) HHH000268: Transaction strategy: org.hibernate.engine.transaction.internal.jta.CMTTransactionFactory
09:42:45,976 INFO [org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory] (MSC service thread 1-7) HHH000397: Using ASTQueryTranslatorFactory
09:42:46,003 INFO [org.hibernate.validator.util.Version] (MSC service thread 1-7) Hibernate Validator 4.2.0.Final
09:42:46,269 INFO [org.hibernate.tool.hbm2ddl.SchemaExport] (MSC service thread 1-7) HHH000227: Running hbm2ddl schema export
09:42:46,275 INFO [stdout] (MSC service thread 1-7) Hibernate: drop table ccmc.users if exists
09:42:46,283 ERROR [org.hibernate.tool.hbm2ddl.SchemaExport] (MSC service thread 1-7) HHH000389: Unsuccessful: drop table ccmc.users if exists
09:42:46,283 ERROR [org.hibernate.tool.hbm2ddl.SchemaExport] (MSC service thread 1-7) Schema "CCMC" not found; SQL statement:
drop table ccmc.users if exists [90079-161]
09:42:46,284 INFO [stdout] (MSC service thread 1-7) Hibernate: create table ccmc.users (user_id decimal(19,2) generated by default as identity, email varchar(100) not null unique, primary key (user_id))
09:42:46,285 ERROR [org.hibernate.tool.hbm2ddl.SchemaExport] (MSC service thread 1-7) HHH000389: Unsuccessful: create table ccmc.users (user_id decimal(19,2) generated by default as identity, email varchar(100) not null unique, primary key (user_id))
09:42:46,285 ERROR [org.hibernate.tool.hbm2ddl.SchemaExport] (MSC service thread 1-7) Schema "CCMC" not found; SQL statement:
create table ccmc.users (user_id decimal(19,2) generated by default as identity, email varchar(100) not null unique, primary key (user_id)) [90079-161]
09:42:46,286 INFO [org.hibernate.tool.hbm2ddl.SchemaExport] (MSC service thread 1-7) HHH000230: Schema export complete
【问题讨论】:
你能发布错误堆栈跟踪吗? 当然...实际上没有任何堆栈跟踪本身,但我发布了服务器输出。当然,稍后当我尝试做某事时,比如保存一个新用户,会有堆栈跟踪,因为“用户”表不存在,但这是意料之中的...... 如果将“create-drop”改为“create”会怎样? 是的,我只是为了好玩而尝试过,但它不起作用。如果 'create-drop' 不起作用,那么 'create' 也不起作用,因为 2 之间的唯一区别是 'create-drop' 会在测试结束时丢弃所有内容,而 'create' 不会。但我的问题是在测试的开始,而不是结束。 【参考方案1】:我在另一个SO Question找到了答案。
在定义到数据库的连接 URL 时,需要添加:“INIT=CREATE SCHEMA IF NOT EXISTS”。因此 persistence.xml 文件中的完整 URL 如下所示:
<connection-url>jdbc:h2:mem:test-db;DB_CLOSE_DELAY=-1;INIT=CREATE SCHEMA IF NOT EXISTS ccmc</connection-url>
其中ccmc
是要创建的模式的名称。参数DB_CLOSE_DELAY
与问题无关,可以安全地忽略此问题。
【讨论】:
如果你想运行多个脚本,你可以这样做:jdbc:h2:mem:test-db;DB_CLOSE_DELAY=-1;INIT=CREATE SCHEMA IF NOT EXISTS ccmc\;CREATE SCHEMA IF NOT EXISTS ccmc2
以上是关于Hibernate SchemaExport 无法首先创建架构的主要内容,如果未能解决你的问题,请参考以下文章
schemaExport 构建失败,带有 Hibernate 的 HelloWorld 程序
hibernate笔记--通过SchemaExport生成数据库表
在 JUnit/Spring 测试期间触发 Hibernate 的 SchemaExport.execute (hbm2ddl.auto) 的原因是啥?
hibernate4.2.2使用schemaExport生成数据表报错,junit提示:SQLNestedException: Cannot create PoolableConnectionFact