当前位置: 首页 > 知识库问答 >
问题:

无法在Spring应用程序中使用Flyway 1.6进行迁移

云开诚
2023-03-14

我使用Hibernate将Roo生成的SpringMVC应用程序连接到PostgreSQL。我试图将Flyway1.6作为一个bean集成到Spring应用程序上下文中。“有什么东西阻止”迁移的执行,我在上下文初始化时遇到了由Hibernate元数据验证引起的错误。使用Flyway 1.6 Maven插件(clean、init、migrate)执行迁移没有问题。

Flyway 1.5(早期版本)的集成工作正常。

我应该怎么做才能使Flyway 1.6集成迁移工作?我应该提供什么额外的配置?

环境:Ubuntu 11.10、Springsource工具套件2.9.1(Eclipse 3.7.2)、OpenJDK 6b23、Tomcat 7.0.23、PostgreSQL 8.4.9/9.1、Spring 3.0/3.1、Hibernate 3.6.4。最后,PostgreSQL本机驱动程序8.4 JDBC3/4构建702。

波姆。xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
...
<dependencies>
    ...
    <dependency>
        <groupId>com.googlecode.flyway</groupId>
        <artifactId>flyway-core</artifactId>
        <version>1.6</version>
        <classifier/>
    </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>com.googlecode.flyway</groupId>
            <artifactId>flyway-maven-plugin</artifactId>
            <version>1.6</version>
            <configuration>
                <driver>org.postgresql.Driver</driver>
                <url>jdbc:postgresql://localhost:5432/library</url>
                <baseDir>db/migration</baseDir>
                <user>library</user>
                <password>library</password>
            </configuration>
        </plugin>
        ...
    </plugins>
</build>
</project>

数据库特性:

database.password=library
database.url=jdbc\:postgresql\://localhost\:5432/library
database.username=library
database.driverClassName=org.postgresql.Driver

坚持不懈xml:

<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="persistenceUnit" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <properties>
        <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>
        <property name="hibernate.hbm2ddl.auto" value="validate"/>
        <property name="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.ImprovedNamingStrategy"/>
        <property name="hibernate.connection.charSet" value="UTF-8"/>
    </properties>
</persistence-unit>
</persistence>

Spring上下文配置:

<beans xmlns="http://www.springframework.org/schema/beans" ...>
<context:property-placeholder location="classpath*:META-INF/spring/*.properties"/>
<context:spring-configured/>
<context:component-scan base-package="edu.sandbox.library">
    <context:exclude-filter expression=".*_Roo_.*" type="regex"/>
    <context:exclude-filter expression="org.springframework.stereotype.Controller" type="annotation"/>
</context:component-scan>

<bean class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" id="dataSource">
    <property name="driverClassName" value="${database.driverClassName}"/>
    <property name="url" value="${database.url}"/>
    <property name="username" value="${database.username}"/>
    <property name="password" value="${database.password}"/>
    <property name="testOnBorrow" value="true"/>
    <property name="testOnReturn" value="true"/>
    <property name="testWhileIdle" value="true"/>
    <property name="timeBetweenEvictionRunsMillis" value="1800000"/>
    <property name="numTestsPerEvictionRun" value="3"/>
    <property name="minEvictableIdleTimeMillis" value="1800000"/>
    <property name="validationQuery" value="SELECT version();"/>
</bean>
<bean class="org.springframework.orm.jpa.JpaTransactionManager" id="transactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<tx:annotation-driven mode="aspectj" transaction-manager="transactionManager"/>
<bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" depends-on="flyway" id="entityManagerFactory">
    <property name="persistenceUnitName" value="persistenceUnit"/>
    <property name="dataSource" ref="dataSource"/>
</bean>
<bean class="com.googlecode.flyway.core.Flyway" id="flyway" init-method="migrate">
    <property name="dataSource" ref="dataSource"/>
</bean>
</beans>

日志:

...
INFO  org.springframework.beans.factory.support.DefaultListableBeanFactory - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@19d03a4e: defining beans [org.springframework.beans.factory.config.PropertyPlaceholderConfigurer#0,org.springframework.context.config.internalBeanConfigurerAspect,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,dataSource,transactionManager,org.springframework.transaction.config.internalTransactionAspect,entityManagerFactory,flyway]; root of factory hierarchy
DEBUG com.googlecode.flyway.core.dbsupport.DbSupportFactory - Database: PostgreSQL
DEBUG com.googlecode.flyway.core.Flyway - Schema: public
DEBUG com.googlecode.flyway.core.util.ClassPathScanner - Scanning directory: file:/home/developer/projects/sandbox/t7-work/webapps/library/WEB-INF/classes/db/migration/
DEBUG com.googlecode.flyway.core.util.ClassPathScanner - Scanning directory: file:/home/developer/projects/sandbox/t7-work/webapps/library/WEB-INF/classes/db/migration/
INFO  org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean - Building JPA container EntityManagerFactory for persistence unit 'persistenceUnit'
...
INFO  org.hibernate.tool.hbm2ddl.DatabaseMetadata - table not found: book
INFO  org.springframework.beans.factory.support.DefaultListableBeanFactory - Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@43a4181c: defining beans [org.springframework.beans.factory.config.PropertyPlaceholderConfigurer#0,org.springframework.context.config.internalBeanConfigurerAspect,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,dataSource,transactionManager,org.springframework.transaction.config.internalTransactionAspect,entityManagerFactory,flyway]; root of factory hierarchy
ERROR org.springframework.web.context.ContextLoader - Context initialization failed org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager' defined in file [/.../applicationContext.xml]: Cannot resolve reference to bean 'entityManagerFactory' while setting bean property 'entityManagerFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in file [/.../applicationContext.xml]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: persistenceUnit] Unable to build EntityManagerFactory
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:328)
...

共有2个答案

葛海阳
2023-03-14

将尾部/添加到baseDir属性值(如前所述)似乎是Flyway 1.6的一个解决方案:

<bean class="com.googlecode.flyway.core.Flyway" id="flyway" init-method="migrate">
    <property name="dataSource" ref="dataSource"/>
    <property name="baseDir" value="db/migration/"/>
</bean>

迁移按预期运行,产生以下日志:

...
DEBUG com.googlecode.flyway.core.dbsupport.DbSupportFactory - Database: PostgreSQL
DEBUG com.googlecode.flyway.core.Flyway - Schema: public
DEBUG com.googlecode.flyway.core.util.ClassPathScanner - Scanning directory: file:/home/developer/projects/sandbox/t7-work/webapps/library/WEB-INF/classes/db/migration/
DEBUG com.googlecode.flyway.core.util.ClassPathScanner - Found resource: db/migration/V1__initial.sql
DEBUG com.googlecode.flyway.core.util.ClassPathScanner - Scanning directory: file:/home/developer/projects/sandbox/t7-work/webapps/library/WEB-INF/classes/db/migration/
DEBUG com.googlecode.flyway.core.util.ClassPathScanner - Scanning directory: file:/home/developer/projects/sandbox/t7-work/webapps/library/WEB-INF/classes/db/migration/
DEBUG com.googlecode.flyway.core.util.ClassPathScanner - Found resource: db/migration/V1__initial.sql
DEBUG com.googlecode.flyway.core.util.ClassPathScanner - Scanning directory: file:/home/developer/projects/sandbox/t7-work/webapps/library/WEB-INF/classes/db/migration/
DEBUG com.googlecode.flyway.core.migration.sql.SqlScript - Found statement at line 17: CREATE TABLE public.schema_version (
...
DEBUG com.googlecode.flyway.core.migration.sql.SqlStatement - Executing SQL: CREATE INDEX schema_version_current_version_index ON public.schema_version (current_version)
INFO  com.googlecode.flyway.core.metadatatable.MetaDataTable - Metadata table created: schema_version (Schema: public)
INFO  com.googlecode.flyway.core.migration.DbMigrator - Current schema version: null
INFO  com.googlecode.flyway.core.migration.DbMigrator - Migrating to version 1
...
INFO  com.googlecode.flyway.core.migration.DbMigrator - Successfully applied 1 migration (execution time 00:00.161s).
...
丌官翰采
2023-03-14

看起来你可能面临与此相同的问题:https://github.com/flyway/flyway/issues/107

请对附带的罐子进行测试。如果它也适用于您,我将在接下来的几天内以1.6.1的形式发布它。

 类似资料:
  • 我无法使用https运行spring启动应用程序。我知道Spring boot使用嵌入式tomcat服务器 下面是stacktrace 下面显示的是我的Yaml文件 我在stackoverflow中尝试了几乎所有的答案 我尝试的一些答案如下: 只有在运行jar文件时才会发生这种情况,我可以从IDE轻松地运行spring启动应用程序(我使用Intellij) > 将端口从8181更改为8443(许多

  • 无法使用maven运行spring启动应用程序。当我运行命令mvn spring boot:run时,我收到:,但在pom中。我写的xml,其中是使用该示例的主要类: 我有这个项目结构: 我希望使用spring boot和maven运行我的应用程序。但现在它不起作用了。

  • 我们正在使用Spring引导将指标发送到应用程序洞察我们正在使用应用程序洞察日志日志日志日志日志日志日志日志日志日志日志日志日志日志日志日志日志日志日志日志日志日志日志日志日志日志日志日志日志日志日志日志日志日志日志日志日志日志日志日志日志日志日志日志。 下面是我们在logj2-spring.xml中使用的附加程序 ** 我们在应用洞察搜索屏幕上看到日志,但是我有几个问题。 > 是否有一种方法可以

  • 我不知道这是否是正确的地方,但无论如何,我正在尝试使用gmail作为我的Spring Boot应用程序中的邮件服务器。当我在本地运行它时,我可以在没有错误的情况下发送邮件。但是当我在我的服务器中部署时。有一个错误。有什么问题吗? 我们已经打开了服务器上的所有端口 这是错误报告:OPPS!未找到内容|无法获取JDBC连接;嵌套的异常是org。冬眠例外GenericJDBCException:无法获取

  • 我将spring boot starter父级修改为2.3.0。通过此修改,我还引入了spring maven插件中的版本 我能够完成..但当我运行命令..我得到以下错误。 Lifecycle.LifecycleExecutionException:无法执行目标org.springframework.boot:spring-boot-maven-plugin:2.3.0。release:在项目上运

  • 我下面介绍了spring的RESTAPI,但我无法让它在tomcat服务器上运行(尝试了tomcat 7和9)。在eclipse中,我确实以- 编辑 尝试在项目上安装mvn,然后java-jarproject.jar 编辑2 我将项目导出到一个可运行的jar(并将所需的库打包到生成的jar中),但是 提供 编辑3 我没有删除我的m2,因为那里有很多东西,但我现在通过eclipse的spring工具