Alex
Alex

Reputation: 617

Spring JPA/HIbernate create/update views when the application starts

Our application is based on spring, JPA, Hibernate (3.5.1), postgresql 8.4

We will deliver a new .war file to our clients, but we have quite a few new views in the DB needed to be created before they can run some reports.

I tried to put an import.sql file under src folder, and configured my persistence.xml like this :

<?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>

and my data-access-context.xml like this:

<?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>

But I cannot get the new views created? any idea? Thanks in advance.

Upvotes: 1

Views: 938

Answers (1)

zellus
zellus

Reputation: 9592

This is not really an answer to your question, but maybe a valuable solution. What about using a database migration framework? I suggest flyway which gives you full access to your database.

During application start (see post) the migration can be triggered using the flyway java api.

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(); 

Upvotes: 1

Related Questions