HSQLDB文件模式不起作用 [英] HSQLDB file mode is not working

查看:123
本文介绍了HSQLDB文件模式不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我在spring-context.xml中有一些bean定义:

 < tx:annotation-driven transaction-manager =transactionManagerproxy-target-class =true/> 

< jdbc:embedded-database id =dataSourcetype =HSQL>
< jdbc:script location =classpath *:database / database_schema.sql/>
< / jdbc:embedded-database>

< beans:bean id =sessionFactoryclass =org.springframework.orm.hibernate5.LocalSessionFactoryBean>
< beans:property name =dataSourceref =dataSource/>
< beans:property name =configLocationvalue =hibernate / hibernate.cfg.xml/>
< beans:property name =annotatedClasses>
< beans:list>
...很多项目bean路径值..
< / beans:list>
< / beans:property>
< / beans:bean>

< beans:bean id =transactionManagerclass =org.springframework.orm.hibernate5.HibernateTransactionManager>
< beans:property name =sessionFactoryref =sessionFactory/>
< / beans:bean>

和Hibernate.cfg.xml包含:

 <?xml version ='1.0'encoding ='utf-8'?> 
<!DOCTYPE hibernate-configuration PUBLIC
- // Hibernate / Hibernate Configuration DTD // EN
http://www.hibernate.org/dtd/hibernate-configuration- 3.0.dtd>
< hibernate-configuration>
< session-factory>
< property name =hibernate.enable_lazy_load_no_trans> true< / property>
< property name =hibernate.dialect> org.hibernate.dialect.HSQLDialect< / property>
< property name =hibernate.hbm2ddl.auto>更新< / property>
< property name =hibernate.show_sql> true< / property>
< property name =connection.driver_class> org.hsqldb.jdbcDriver< / property>
< property name =connection.url> jdbc:hsqldb:file:D / database< / property>
< property name =connection.pool_size> 10< / property>
< property name =connection.username> SA< / property>
< property name =connection.password>< / property>
< / session-factory>
< / hibernate-configuration>

所有dao和服务图层都正常工作。数据总是保存,一切都正常。

但在下次启动时,所有数据都将从数据库中删除。

我试图将我的数据库数据保存到文件中并再次使用这些数据,并尝试了很多变体。最后一个来自关于HSQLDB url config



我已将该行添加到我的hibernate.cfg.xml文件中:

 < property name =connection.url> jdbc:hsqldb:file:D / database< / property> 

这对我没有帮助。此路径中没有创建文件。



另外我试图使用问题并使用SCRIPT SQL命令,但我不完全理解如何正确使用它,以及我必须在哪里直接放置它?



问题:如何配置HSQLDB以在关闭应用程序后将我的数据库中的所有数据保存到文件中, - 在再次打开此应用程序后使用此数据?
你可以用这个问题的正确配置HSQLDB来写一些小而有效的例子吗?



编辑#1:
谢谢jchampemont,我尝试了你的变体和你的所有步骤,但仍然没有。该文件仍然不存在:(
Hibernate文件被选中,所有的休眠配置工作正常。



问题已修复! jchampemont和fredt



我忘记删除hibernate.cfg.xml中的这些值:

 < property name =connection.driver_class> org.hsqldb.jdbcDriver< / property> 
< property name =connection.url> ; jdbc:hsqldb:file:D:/database.lck< / property>
< property name =connection.pool_size> 10< / property>
< property name =连接。 < / property>

我创建了bean数据源,如 jchampemont答案
删除后这4行 - 一切都开始正常工作,基地创建在D:\数据库文件夹一切都很酷。感谢您的回答!和平:)

解决方案

您确定您的 Hibernate.cfg.xml 春天被拾起?根据文件的位置,您可能需要为其位置添加 classpath:前缀。



使用 jdbc:embedded-database ,您还可以执行以下操作来创建DataSource bean并指定数据库URL:

 < bean class =org.apache.commons.dbcp.BasicDataSourcedestroy-method =closeid =dataSource> 
< property name =driverClassNamevalue =org.hsqldb.jdbcDriver/>
< property name =urlvalue =jdbc:hsqldb:file:D:/ database/>
< property name =usernamevalue =sa/>
< property name =passwordvalue =/>
< / bean>


< jdbc:script location =classpath *:database / database_schema.sql/>
< / jdbc:initialize-database>


I am using HSQLDB with Hibernate 5 and Spring 4.

I have some beans definitions in my spring-context.xml:

<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>

    <jdbc:embedded-database id="dataSource" type="HSQL">
        <jdbc:script location="classpath*:database/database_schema.sql"/>
    </jdbc:embedded-database>

    <beans:bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        <beans:property name="dataSource" ref="dataSource"/>
        <beans:property name="configLocation" value="hibernate/hibernate.cfg.xml"/>
        <beans:property name="annotatedClasses">
            <beans:list>
               ...lot of project beans path values..
            </beans:list>
        </beans:property>
    </beans:bean>

    <beans:bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
        <beans:property name="sessionFactory" ref="sessionFactory"/>
    </beans:bean>

And Hibernate.cfg.xml contains:

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.enable_lazy_load_no_trans">true</property>
        <property name="hibernate.dialect">org.hibernate.dialect.HSQLDialect</property>
        <property name="hibernate.hbm2ddl.auto">update</property>
        <property name="hibernate.show_sql">true</property>
        <property name="connection.driver_class">org.hsqldb.jdbcDriver</property>
        <property name="connection.url">jdbc:hsqldb:file:D/database</property>
        <property name="connection.pool_size">10</property>
        <property name="connection.username">SA</property>
        <property name="connection.password"></property>
    </session-factory>
</hibernate-configuration>

All dao and services layers is working fine. Data is always saved and all is all right.

But in the next launch all data is deleting from database.

I am trying to save my database data in file and to use this data again, and I have tried many variants. The last of them is from question about HSQLDB url config

I have added the line to my hibernate.cfg.xml file:

 <property name="connection.url">jdbc:hsqldb:file:D/database</property>

It doesn't helped to me. There is no file creating in this path.

Also i am trying to use the advice from question and to use SCRIPT SQL command, but I do not fully understand how to properly use it and where i must to place it directly?

Question: How should I configure HSQLDB to save all data from my database to a file after closing app and to re-use this data after opening this application again? Can you write some small and valid example with the correct configuration of HSQLDB for this question?

Edit #1: Thank's jchampemont, i tried your variant and all your steps, but still nothing. The file is still doesn't exists :( Hibernate file is picked, all hibernate config is working fine.

Issue is fixed! Thank's to jchampemont and to fredt

I have forgot to delete this values in hibernate.cfg.xml...:

        <property name="connection.driver_class">org.hsqldb.jdbcDriver</property>
        <property name="connection.url">jdbc:hsqldb:file:D:/database.lck</property>
        <property name="connection.pool_size">10</property>
        <property name="connection.username">SA</property>
        <property name="connection.password"></property>

And i have created the bean dataSource like in jchampemont answer After deleting this 4 lines - everything start's to work fine, base were created in D:\database folder and everything is cool. Thank's for answers! Peace :)

解决方案

Are you sure your Hibernate.cfg.xml is being picked up by Spring? Depending on where the file is, you may need to add a classpath: prefix to its location.

Instead of using the jdbc:embedded-database, you could also do the following to create a DataSource bean and specify the database URL:

<bean class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" id="dataSource">
    <property name="driverClassName" value="org.hsqldb.jdbcDriver" />
    <property name="url" value="jdbc:hsqldb:file:D:/database" />
    <property name="username" value="sa" />
    <property name="password" value="" />
</bean>


<jdbc:initialize-database data-source="dataSource">
    <jdbc:script location="classpath*:database/database_schema.sql" />
 </jdbc:initialize-database>

这篇关于HSQLDB文件模式不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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