Spring JPA / HIbernate создает / обновляет представления при запуске приложения - PullRequest
1 голос
/ 02 марта 2012

Наше приложение основано на Spring, JPA, Hibernate (3.5.1), postgresql 8.4

Мы доставим новый .war-файл нашим клиентам, но у нас есть немало новых представлений в БД, которые необходимо создать, прежде чем они смогут запускать некоторые отчеты.

Я попытался поместить файл import.sql в папку src и настроил свой файл persistence.xml следующим образом. :

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.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_1_0.xsd">
    <persistence-unit name="myApp">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <class>....</class>

       <properties>
           <property name="hibernate.hbm2ddl.auto" value="update" />
             <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.cache.provider_class" value="org.hibernate.cache.HashtableCacheProvider" />
        </properties>       
    </persistence-unit>
</persistence>

и мой data-access-context.xml вот так:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
                           http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/context 
                           http://www.springframework.org/schema/context/spring-context.xsd
                           http://www.springframework.org/schema/tx
                           http://www.springframework.org/schema/tx/spring-tx.xsd">


    <bean id="dataSource"
          class="org.springframework.jndi.JndiObjectFactoryBean">
          <property name="jndiName"
                      value="java:comp/env/jdbc/mydb"/>   
          <property name="resourceRef"
                      value="true" />
    </bean> 

    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
            <property name="dataSource" ref="dataSource" />
            <property name="persistenceUnitName" value="myApp" />
            <property name="jpaVendorAdapter">
                    <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                            <property name="generateDdl" value="true" />
                            <property name="showSql" value="true" />
                            <property name="databasePlatform" value="org.hibernate.dialect.PostgreSQLDialect" />
                    </bean>
            </property>
    </bean>

    <tx:annotation-driven/>

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory"/>
    </bean>

    <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />

    <bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>

    <context:component-scan base-package="com.pb.redline.dao" />    


</beans>

Но я не могу создать новые представления? любая идея? Заранее спасибо.

1 Ответ

1 голос
/ 02 марта 2012

Это не совсем ответ на ваш вопрос, но, возможно, ценное решение. Как насчет использования инфраструктуры миграции баз данных? Я предлагаю flyway , который дает вам полный доступ к вашей базе данных.

Во время запуска приложения (см. post ) миграция может быть запущена с помощью API Java flyway.

Properties properties = new Properties();
properties.setProperty("flyway.user", "postgres");
properties.setProperty("flyway.password", "secret");
properties.setProperty("flyway.url", "jdbc:postgresql://localhost/database-name");
properties.setProperty("flyway.driver", "org.postgresql.Driver");

flyway = new Flyway();
flyway.configure(properties);
flyway.clean();
flyway.migrate(); 
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...