启动脚本在HSQLDB中创建一个模式 [英] Startup script to create a schema in HSQLDB

查看:69
本文介绍了启动脚本在HSQLDB中创建一个模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我使用Spring并且有很多与 import.sql 的数据库交互,但是,在Hibernate有创建了所有表格。我试图使用HSQLDB应该读取的 .script 文件,但我认为当您使用内存不足的数据库时无法工作。



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

解决方案

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

 <?xml version =1.0encoding =UTF-8?> 
< beans xmlns =http://www.springframework.org/schema/beans
xmlns:xsi =http://www.w3.org/2001/XMLSchema-instancexmlns :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 =dataSourcetype =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  - 创建嵌入式数据库'dataSource'
2011-11-01 19:10:08,583 [main ] INFO org.springframework.jdbc.datasource.init.ResourceDatabasePopulator - 从类路径资源执行SQL脚本[my.sql]
2011-11-01 19:10:08,683 [main] INFO org.springframework.jdbc。 datasource.init.ResourceDatabasePopulator - 完成在100 ms内从类路径资源[my.sql]执行SQL脚本。
2011-11-01 19:10:08,683 [main] INFO org.springframework.context.support.GenericApplicationContext - [类org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseFactoryBean]类型的Bean'dataSource'不是有资格获得所有BeanPostProcessors处理(例如:不适合自动代理)
2011-11-01 19:10:08,683 [main] INFO org.springframework.context.support.GenericApplicationContext - Bean'dataSource' [class org.springframework.jdbc.datasource.SimpleDriverDataSource]不符合所有BeanPostProcessors处理的条件(例如:不符合自动代理条件)
2011-11-01 19:10:08,700 [main ] INFO org.springframework.context.support.GenericApplicationContext - 类org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter的Bean'org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter#35712651'没有资格获取处理由所有的BeanPostProcessors(例如:不符合条件的uto-proxying)
2011-11-01 19:10:08,717 [main] INFO org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean - 为持久性单元'testH2DbPersistenceUnit'构建JPA容器EntityManagerFactory
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
...
2011-11-01 19:10:10,313 [main] INFO org.hibernate.tool.hbm2ddl.SchemaUpdate - 运行hbm2ddl模式更新
2011-11-01 19:10:10,313 [main] INFO org。 hibernate.tool.hbm2ddl.SchemaUpdate - 获取数据库元数据
2011-11-01 19:10:10,315 [main] INFO org.hibernate.tool.hbm2ddl.SchemaUpdate - 更新模式


I am attempting to use an in-memory database to mock out a teradata database. I need to create a schema before the tables are built, however, it is giving me fits.

I am using Spring and have lots of database interaction with import.sql, however, this executes after Hibernate has created all the tables. I have attempted to use the .script file that HSQLDB is supposed to read, but I think when you use an in-memory database that doesn't work. I have moved the files around a bit and nothing seems to hit.

Anyone figured out how to create a schema on startup for an in-memory HSQLDB database?

解决方案

If you run your script this way...

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

...then it is executed before Hibernate does its initialization stuff.


I have tested it again, especially for you. It is run before Hibernate creates the tables. See this log (running the script is in the first 3 lines, Hibernate in the last ones):

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

这篇关于启动脚本在HSQLDB中创建一个模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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