我想夾具/樣品數據添加到使用Hibernate作爲JPA一個Spring項目。如何燈具添加到春季Hibernate項目
我創建了一個fixtures.sql文件(在WEB-INF /班/ fixtures.sql)包含了一堆,我要執行我的同時發展與樣本數據來填充數據庫的SQL查詢。 在發展模式,我在內存數據庫超音速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>
有了一些調整,我得到了import.sql工作的,萬分感謝! 不得不添加一行到我的persistence.xml來讓它工作: –
2011-03-13 19:07:31
出於好奇,什麼是初始數據被稱爲?在Pylons框架(一個python框架)中工作時,它被稱爲fixtures,Spring/Hibernate使用相同的術語嗎? – 2011-03-13 19:17:58
我不認爲Spring/Hibernate將它稱爲與夾具不同的東西。這是這類事情的通常術語。或者說,這些數據是測試夾具的一部分。 – sourcedelica 2011-03-13 19:45:21