我想在内存DB中设置一个带有hibernate,spring mvc和H2的项目(现在)。 当一切都启动(在码头)我得到错误,表示表尚不存在。
这是我得到的错误:
Okt 09, 2013 3:42:47 PM org.hibernate.tool.hbm2ddl.SchemaExport execute
INFO: HHH000227: Running hbm2ddl schema export
Okt 09, 2013 3:42:47 PM org.hibernate.tool.hbm2ddl.SchemaExport perform
ERROR: HHH000389: Unsuccessful: alter table PUBLIC.Object drop constraint FK_9sd2x4ehbugdjyrp1xqmsjw00
Okt 09, 2013 3:42:47 PM org.hibernate.tool.hbm2ddl.SchemaExport perform
ERROR: Tabelle "OBJECT" nicht gefunden
Table "OBJECT" not found; SQL statement:
...
如果我没有设置Hibernate,那就没关系了:
hibernate.hbm2ddl.auto=create
,H2的连接字符串是:
jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=TRUE
我可以使用IntelliJ连接到数据库,所以它就在那里..
我希望Hibernate为我生成表,然后是约束和其他任何后续的但是由于某种原因它不会那样做。它可能是用户权限吗?这些是我设置的用户设置:
jdbc.user=sa
jdbc.pass=
任何帮助表示赞赏! 谢谢:))
更新
如果我将连接字符串更改为:
jdbc.url=jdbc:h2:~/db/database.db;DB_CLOSE_ON_EXIT=TRUE;INIT=create schema IF NOT EXISTS generic;
它似乎有效。但是为什么它在内存中不起作用以及为什么它在我将默认模式设置为“公共”之前不起作用(这已经是我已经想到的可能只是将我的东西转储到那里......)IDK!
答案 0 :(得分:0)
这听起来像https://hibernate.atlassian.net/browse/HHH-7002。
就我而言,我可以通过配置Hibernate来通过hibernate.dialect
中的persistence.xml
属性使用以下类来消除异常:
import org.hibernate.dialect.H2Dialect;
// This class is a workaround for https://hibernate.atlassian.net/browse/HHH-7002
public class CustomH2Dialect extends H2Dialect {
@Override
public String getDropSequenceString(String sequenceName) {
// Adding the "if exists" clause to avoid warnings
return "drop sequence if exists " + sequenceName;
}
@Override
public boolean dropConstraints() {
// We don't need to drop constraints before dropping tables; that just
// leads to error messages about missing tables when we don't have a
// schema in the database
return false;
}
@Override
public boolean supportsIfExistsBeforeTableName() {
return true;
}
@Override
public boolean supportsIfExistsAfterTableName() {
return false;
}
@Override
public String getCascadeConstraintsString() {
return " CASCADE ";
}
}
答案 1 :(得分:0)
除了使用:
<property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
<property name="hibernate.hbm2ddl.auto" value="create-drop" />
<?xml version="1.0" encoding="UTF-8" ?>
<persistence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0" xmlns="http://java.sun.com/xml/ns/persistence">
<persistence-unit name="testPU" transaction-type="RESOURCE_LOCAL">
<description>Hibernate test case template Persistence Unit</description>
<class>com.domain.A</class>
<class>com.domain.B</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="hibernate.connection.url" value="jdbc:h2:mem:test;MODE=PostgreSQL;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE;DATABASE_TO_UPPER=false" />
<property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="false" />
<property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
<property name="javax.persistence.schema-generation.scripts.action" value="drop-and-create" />
<property name="javax.persistence.schema-generation.scripts.create-target" value="db-schema.jpa.ddl" />
<property name="javax.persistence.schema-generation.scripts.drop-target" value="db-schema.jpa.ddl" />
</properties>
</persistence-unit>
</persistence>