在 LightAdmin (Spring/JPA/Hibernate) 中使 HSQL 数据库持久化
Posted
技术标签:
【中文标题】在 LightAdmin (Spring/JPA/Hibernate) 中使 HSQL 数据库持久化【英文标题】:Make HSQL database persistent in LightAdmin (Spring/JPA/Hibernate) 【发布时间】:2014-03-13 00:00:55 【问题描述】:我正在尝试将使用内存 HSQL 数据库的默认 LightAdmin 设置更改为使用文件 HSQL 数据库。
persistence.xml:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="DEMO" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>org.lightadmin.demo.model.Address</class>
<class>org.lightadmin.demo.model.Customer</class>
<class>org.lightadmin.demo.model.EmailAddress</class>
<class>org.lightadmin.demo.model.Product</class>
<class>org.lightadmin.demo.model.Order</class>
<class>org.lightadmin.demo.model.LineItem</class>
<class>org.lightadmin.demo.model.DiscountProgram</class>
</persistence-unit>
</persistence>
spring-context.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="org.lightadmin.demo.service"/>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
p:persistenceUnitName="DEMO" p:dataSource-ref="dataSource">
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="generateDdl" value="true"/>
<property name="database" value="HSQL"/>
</bean>
</property>
</bean>
<jdbc:embedded-database id="dataSource" type="HSQL">
<jdbc:script location="classpath:db/schema.sql" encoding="UTF-8"/>
<jdbc:script location="classpath:db/data.sql" encoding="UTF-8"/>
</jdbc:embedded-database>
</beans>
我尝试过的:在 persistence.xml 中提供了以下属性:
<property name="hibernate.connection.url" value="jdbc:hsqldb:file:database"/>
<property name="hibernate.hbm2ddl.auto" value="update" />
但它没有完成它的工作。
【问题讨论】:
【参考方案1】:找到了解决办法,把spring-context改成:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
p:persistenceUnitName="DEMO">
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="true" />
<property name="generateDdl" value="true" />
<property name="database" value="HSQL" />
</bean>
</property>
</bean>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.hsqldb.jdbcDriver" />
<property name="url" value="jdbc:hsqldb:file:build_database" />
<property name="username" value="sa" />
<property name="password" value="" />
</bean>
</beans>
【讨论】:
以上是关于在 LightAdmin (Spring/JPA/Hibernate) 中使 HSQL 数据库持久化的主要内容,如果未能解决你的问题,请参考以下文章
Spring Data 系列 Spring+JPA(spring-data-commons)