7

我想在内存数据库中建立一个带有休眠、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;

它似乎工作。但是为什么它在内存中不起作用以及为什么在我将默认模式设置为“public”之前它不起作用(它已经存在所以我想不妨把我的东西转储在那里......)IDK!

4

2 回答 2

2

除了使用:

  • DB_CLOSE_DELAY=-1;
  • DB_CLOSE_ON_EXIT=假;
  • DATABASE_TO_UPPER=假

使用这个 JPA 属性

<property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>

代替这个休眠属性:

<property name="hibernate.hbm2ddl.auto" value="create-drop" />

内存persistence.xml示例中的工作H2:

<?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>
于 2018-06-12T03:03:25.547 回答
0

这听起来像https://hibernate.atlassian.net/browse/HHH-7002

就我而言,我可以通过以下hibernate.dialect属性将 Hibernate 配置为使用以下类来消除异常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 ";
    }
}

(感谢https://stackoverflow.com/a/20698339/14379

于 2014-06-18T07:02:37.133 回答