【问题标题】:H2 in-mem-DB with hibernate set to create giving me table not found errorsH2 in-mem-DB with hibernate set to create give me table not found 错误
【发布时间】:2013-10-16 22:33:12
【问题描述】:

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

【问题讨论】:

  • 为什么 Hibernate 会在空数据库上删除外键?尝试启用 SQL 日志记录以查看 Hibernate 如何尝试检查现有数据库模式。
  • 一开始它甚至没有尝试创建表或检查是否存在它只是尝试直接删除键。见鬼,我不知道为什么会这样。我在数据库上的“第一次”运行时仍然遇到同样的错误,但在那之后,表格就在那里并且可以与它们一起使用......非常奇怪
  • 错误提示您没有使用您认为的数据库。再次检查配置,设置断点并调试。
  • 你弄明白了吗?

标签: hibernate h2


【解决方案1】:

除了使用:

  • DB_CLOSE_DELAY=-1;
  • DB_CLOSE_ON_EXIT=FALSE;
  • DATABASE_TO_UPPER=false

USE this JPA PROPERTY:

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

代替这个 HIBERNATE 属性:

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

在 Memory 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>

【讨论】:

    【解决方案2】:

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

    就我而言,我可以通过persistence.xml 中的hibernate.dialect 属性将Hibernate 配置为使用以下类来消除异常:

    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

    【讨论】:

      猜你喜欢
      • 2012-10-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-01
      • 2020-12-12
      • 2019-05-12
      • 2015-10-19
      • 1970-01-01
      相关资源
      最近更新 更多