用于在HSQLDB中创建模式的启动脚本

时间:2011-11-01 14:34:16

标签: hibernate spring hsqldb

我正在尝试使用内存数据库来模拟teradata数据库。我需要在构建表之前创建一个模式,但是,它给了我适合。

我正在使用Spring并且与import.sql有很多数据库交互,但是,这在Hibernate创建了所有表之后执行。我试图使用HSQLDB应该读取的.script文件,但我认为当你使用内存数据库不起作用时。我把文件移动了一下,似乎什么也没发现。

有人想出如何在启动时为内存中的HSQLDB数据库创建架构吗?

2 个答案:

答案 0 :(得分:5)

如果你以这种方式运行脚本......

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd">


<jdbc:embedded-database id="dataSource" type="H2" >
    <jdbc:script location="classpath:my.sql" />     
</jdbc:embedded-database>   

...然后在Hibernate执行初始化之前执行它。


我再次测试过,尤其是对你而言。它在Hibernate创建表之前运行。请参阅此日志(运行脚本位于前3行,Hibernate位于最后一行):

2011-11-01 19:10:08,380 [main] INFO  org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseFactory - Creating embedded database 'dataSource'
2011-11-01 19:10:08,583 [main] INFO  org.springframework.jdbc.datasource.init.ResourceDatabasePopulator - Executing SQL script from class path resource [my.sql]
2011-11-01 19:10:08,683 [main] INFO  org.springframework.jdbc.datasource.init.ResourceDatabasePopulator - Done executing SQL script from class path resource [my.sql] in 100 ms.
2011-11-01 19:10:08,683 [main] INFO  org.springframework.context.support.GenericApplicationContext - Bean 'dataSource' of type [class org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseFactoryBean] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2011-11-01 19:10:08,683 [main] INFO  org.springframework.context.support.GenericApplicationContext - Bean 'dataSource' of type [class org.springframework.jdbc.datasource.SimpleDriverDataSource] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2011-11-01 19:10:08,700 [main] INFO  org.springframework.context.support.GenericApplicationContext - Bean 'org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter#35712651' of type [class org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2011-11-01 19:10:08,717 [main] INFO  org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean - Building JPA container EntityManagerFactory for persistence unit 'testH2DbPersistenceUnit'
2011-11-01 19:10:08,854 [main] INFO  org.hibernate.annotations.common.Version - Hibernate Commons Annotations 3.2.0.Final
2011-11-01 19:10:08,859 [main] INFO  org.hibernate.cfg.Environment - Hibernate 3.6.7.Final
2011-11-01 19:10:08,861 [main] INFO  org.hibernate.cfg.Environment - hibernate.properties not found
...
2011-11-01 19:10:10,313 [main] INFO  org.hibernate.tool.hbm2ddl.SchemaUpdate - Running hbm2ddl schema update
2011-11-01 19:10:10,313 [main] INFO  org.hibernate.tool.hbm2ddl.SchemaUpdate - fetching database metadata
2011-11-01 19:10:10,315 [main] INFO  org.hibernate.tool.hbm2ddl.SchemaUpdate - updating schema

答案 1 :(得分:0)

您可以使用文件:数据库,其中包含内存中的所有数据,并且不会对您的测试进行任何更改。这应该符合你的目的。

首先使用网址jdbc:hsqldb:file:initdata创建数据库架构并执行SHUTDOWN。然后加 files_readonly=trueinitdata.properties文件,或者使用以下网址:

jdbc:hsqldb:file:initdata;files_readonly=true

当您针对此数据库运行测试时,不会将任何内容写入数据库文件,并且所有数据都将存储在内存中。