【发布时间】:2011-03-13 12:37:35
【问题描述】:
我正在尝试将固定装置/示例数据添加到使用 Hibernate 作为 JPA 的 Spring 项目中。
我创建了一个 fixtures.sql 文件(在 WEB-INF/classes/fixtures.sql 中),其中包含我想在开发时执行这些 SQL 查询以用示例数据填充数据库。 在开发模式下,我使用内存数据库 Hypersonic InMemory,当我部署到 Postgres 或 MySQL 时,将加载相同的数据。
当应用程序重新启动/启动时如何加载这些数据?每次应用程序重新启动时,Hypersonic 都会擦除内存中的数据库,这意味着每次我想在前端测试某些东西时,我都需要完成所有步骤来创建示例数据。
database.properties:
database.password=
database.url=jdbc\:hsqldb\:mem\:myproject
database.username=sa
database.driverClassName=org.hsqldb.jdbcDriver
persistence.xml
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="persistenceUnit" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
<!-- value="create" to build a new database on each run; value="update" to modify an existing database; value="create-drop" means the same as "create" but also drops tables when Hibernate closes; value="validate" makes no changes to the database -->
<property name="hibernate.hbm2ddl.auto" value="create"/>
<property name="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.ImprovedNamingStrategy"/>
<property name="hibernate.connection.charSet" value="UTF-8"/>
</properties>
</persistence-unit>
</persistence>
【问题讨论】: