我们的应用程序基于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>
但是我无法创建新视图? 任何的想法?提前谢谢。
答案 0 :(得分:1)
这不是你问题的真正答案,但也许是一个有价值的解决方案。那么使用数据库迁移框架呢?我建议使用flyway,它可以让您完全访问您的数据库。
在应用程序启动期间(请参阅post),可以使用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();