H2 in-mem-DB 与 hibernate 设置为创建给我表未找到错误

2024-03-04

我想在内存数据库中建立一个包含 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=

任何帮助表示赞赏! 谢谢 :)

UPDATE

如果我将连接字符串更改为:

jdbc.url=jdbc:h2:~/db/database.db;DB_CLOSE_ON_EXIT=TRUE;INIT=create schema IF NOT EXISTS generic;

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


除了使用:

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

使用此 JPA 属性 https://docs.oracle.com/javaee/7/tutorial/persistence-intro005.htm:

<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>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

H2 in-mem-DB 与 hibernate 设置为创建给我表未找到错误 的相关文章

随机推荐