【问题标题】:Startup script to create a schema in HSQLDB在 HSQLDB 中创建模式的启动脚本
【发布时间】:2011-12-19 13:32:30
【问题描述】:

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

我正在使用 Spring 并且与 import.sql 有很多数据库交互,但是,这在 Hibernate 创建所有表之后执行。我试图使用 HSQLDB 应该读取的.script 文件,但我认为当您使用不起作用的内存数据库时。我已经移动了一些文件,但似乎没有任何影响。

有人知道如何在启动时为内存中的 HSQLDB 数据库创建模式吗?

【问题讨论】:

    标签: hibernate spring hsqldb


    【解决方案1】:

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

    <?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
    

    【讨论】:

    • 这发生在一切都完成后。错误的答案。对不起。但它很酷......
    • @markthegrea:奇怪的是,当我尝试它时,它是在休眠创建表之前完成的。为了证明这一点,我附上了日志。所以你的问题一定是别的。 -- 正确答案。
    • 拉尔夫,我想通了。我在创建数据源之后放置了您给我的代码。我花了一些时间来获取 Spring 日志记录,但位置肯定有所不同。如果你把它放在数据源 xml 之前,它会像宣传的那样工作。我不知道订单在春季有所不同。此外,Spring 似乎默认为“jdbc:hsqldb:mem:testdb”,特别是“testdb”。我的命名方式不同,Spring 在内存中启动了“testdb”。当我将我的更改为“testdb”时,宾果游戏一切正常。谢谢!
    • 还有一个提示。 也必须在 标签之后。组件扫描会触发很多东西,如果是之前,脚本会运行到很晚。只要确保您的数据库首先启动...
    【解决方案2】:

    您可以使用一个文件:数据库,其中所有数据都在内存中,并且您的测试没有任何更改。这应该符合您的目的。

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

    jdbc:hsqldb:file:initdata;files_readonly=true
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-18
      • 2016-06-14
      • 2017-07-02
      相关资源
      最近更新 更多