在LightAdmin中使HSQL数据库持久化(Spring / JPA / Hibernate)

时间:2014-03-13 00:00:55

标签: spring hibernate jpa hsqldb lightadmin

我正在尝试更改使用内存中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>

弹簧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 个答案:

答案 0 :(得分:0)

找到一个解决方案,将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>