java - How to populate H2 in-memory DB from a file (populate.sql)? -
i want add h2 in-memory db spring mvc application. orm i’m using hibernate. here configs:
[hibernate.cfg.xml]
<?xml version="1.0" encoding="utf-8"?> <!doctype hibernate-configuration public "-//hibernate/hibernate configuration dtd 3.0//en" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="hibernate.connection.driver_class">org.h2.driver</property> <property name="hibernate.connection.url">jdbc:h2:~/mydb</property> <property name="hibernate.dialect">org.hibernate.dialect.h2dialect</property> <property name="hibernate.connection.username">sa</property> <property name="hibernate.connection.password">qwerty</property> <property name="show_sql">true</property> <property name="hbm2ddl.auto">create</property> <mapping class="de.alf.h2.entity.student"/> </session-factory> </hibernate-configuration>
[data.xml]
<bean:beans xmlns:bean="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:ctx="http://www.springframework.org/schema/context" xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <tx:annotation-driven transaction-manager="transactionmanager"/> <ctx:component-scan base-package="de.alf.h2.repository"/> <bean:bean id="template" class="org.springframework.orm.hibernate3.hibernatetemplate"> <bean:property name="sessionfactory" ref="sessionfactory"/> </bean:bean> <bean:bean id="sessionfactory" class="org.springframework.orm.hibernate3.annotation.annotationsessionfactorybean"> <bean:property name="configlocation" value="classpath:/hibernate/hibernate.cfg.xml"/> </bean:bean> <bean:bean id="transactionmanager" class="org.springframework.orm.hibernate3.hibernatetransactionmanager"> <bean:property name="sessionfactory" ref="sessionfactory"/> </bean:bean> </bean:beans>
[web.xml]
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <servlet> <servlet-name>mvc-dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class> <init-param> <param-name>contextconfiglocation</param-name> <param-value>/web-inf/mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>mvc-dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <context-param> <param-name>contextconfiglocation</param-name> <param-value>/web-inf/data.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.contextloaderlistener</listener-class> </listener> </web-app>
i need create db file (resource/db/data.sql).
create table student { id int not null auto_increment, name varchar (250) }
how in code?
you can use jdbc namespace xmlns:jdbc="http://www.springframework.org/schema/jdbc"
can add embedded database
<jdbc:embedded-database id="datasource"> <jdbc:script location="classpath:db/schema.sql"/> <jdbc:script location="classpath:db/test-data.sql"/> </jdbc:embedded-database>
then use data source in jdbctemplate or in jpa hibernate
Comments
Post a Comment