在内存数据库中使用Hibernate和H2时出错 [英] Error using Hibernate with H2 in memory database

查看:159
本文介绍了在内存数据库中使用Hibernate和H2时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Hibernate。我如何配置我的persistence.xml有一个H2内存数据库?



我的 persistence.xml 是:

 <?xml version =1.0encoding =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.0xmlns =http://java.sun.com/xml/ns/persistence >
< persistence-unit name =persistenceUnit
transaction-type =RESOURCE_LOCAL>

< class> com.mastertheboss.domain.Employee< / class>
< class> com.mastertheboss.domain.Department< / class>
<属性>

< property name =javax.persistence.jdbc.drivervalue =org.h2.Driver/>
< property name =javax.persistence.jdbc.urlvalue =jdbc:h2:mem:test; DB_CLOSE_DELAY = -1/>
< property name =javax.persistence.jdbc.uservalue =sa/>
< property name =javax.persistence.jdbc.passwordvalue =/>
< property name =hbm2ddl.autovalue =update/>

< property name =hibernate.dialectvalue =org.hibernate.dialect.H2Dialect/>
< / properties>

< / persistence-unit>
< /余辉>

但是当我运行我的应用程序时,出现以下错误 p>

内部异常:org.h2.jdbc.JdbcSQLException:未找到表EMPLOYEE; SQL语句:
选择ID,姓名,DEPARTMENT_ID FROM EMPLOYEE [42102-171]
错误代码:42102
召唤:SELECT ID,姓名,DEPARTMENT_ID FROM EMPLOYEE
查询:ReadAllQuery( referenceClass =雇员SQL = SELECT ID,NAME,DEPARTMENT_ID FROM EMPLOYEE)

解决方案

您应该将hibernate.hbm2ddl.auto属性设置为在您第一次运行应用程序时创建,以创建表格

 <属性name =hibernate.hbm2ddl.autovalue =create/> 

然后(如果您不希望每次启动时重新创建和清空表)将其设置为验证。

 < property name =hibernate.hbm2ddl.autovalue =validate/> gt ; 

要自动创建模式,请将if- not-exists添加到连接url中,如下所示:

 < property name =hibernate.connection.urlvalue =jdbc:h2:〜/< filename>; INIT = CREATE SCHEMA IF NOT EXISTS< schema_name> /> 


I'm working with Hibernate. How can I configure my persistence.xml to have an H2 in-memory database?

My persistence.xml is:

<?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="persistenceUnit"
        transaction-type="RESOURCE_LOCAL">

        <class>com.mastertheboss.domain.Employee</class>
        <class>com.mastertheboss.domain.Department</class>
        <properties>

            <property name="javax.persistence.jdbc.driver" value="org.h2.Driver" />
            <property name="javax.persistence.jdbc.url" value="jdbc:h2:mem:test;DB_CLOSE_DELAY=-1" />
            <property name="javax.persistence.jdbc.user" value="sa" />
            <property name="javax.persistence.jdbc.password" value="" />
            <property name="hbm2ddl.auto" value="update" />

            <property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect" />
        </properties>

    </persistence-unit>
</persistence>

But when I run my app I get the following error:

Internal Exception: org.h2.jdbc.JdbcSQLException: Table "EMPLOYEE" not found; SQL statement: SELECT ID, NAME, DEPARTMENT_ID FROM EMPLOYEE [42102-171] Error Code: 42102 Call: SELECT ID, NAME, DEPARTMENT_ID FROM EMPLOYEE Query: ReadAllQuery(referenceClass=Employee sql="SELECT ID, NAME, DEPARTMENT_ID FROM EMPLOYEE")

解决方案

You should set hibernate.hbm2ddl.auto property to "create" the first time you run your application, to create the tables

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

and then (if you don't want the tables to be recreated and emptied every time you start) set it to "validate".

<property name="hibernate.hbm2ddl.auto" value="validate" />

To create the schema automatically, add if-not-exists to your connection url like this:

<property name="hibernate.connection.url" value="jdbc:h2:~/<filename>;INIT=CREATE SCHEMA IF NOT EXISTS <schema_name>" />

这篇关于在内存数据库中使用Hibernate和H2时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆