【问题标题】:Why flyway did not create a table?为什么flyway没有创建表?
【发布时间】:2020-02-15 12:37:46
【问题描述】:

我最近在 springboot 中使用 flyway,我有这样一个问题。为什么flyway没有创建数据库?我刚刚在我的 build.gradle 中添加了一个 flyway 依赖项,现在看起来像这样。

dependencies {
    implementation 'org.springframework:spring-orm:5.1.5.RELEASE'
    implementation 'org.hibernate.search:hibernate-search-backend-lucene:6.0.0.Beta1'
    implementation 'org.hibernate.search:hibernate-search-mapper-orm:6.0.0.Beta1'
    implementation 'org.postgresql:postgresql:42.2.5'

    compile group: 'org.springframework.boot', name: 'spring-boot-starter-jdbc', version: '2.1.8.RELEASE'
    compile group: 'com.vividsolutions', name: 'jts', version: '1.13'
    compile('org.flywaydb:flyway-core:6.0.6')
}

我这样配置数据源:

@Configuration
@EnableTransactionManagement
@PropertySource("classpath:hibernate.properties")
public class HibernateConfig {

    @Autowired
    private Environment env;

    @Bean
    public DataSource dataSource() {
        final DataSourceBuilder dataSourceBuilder = DataSourceBuilder.create();
        dataSourceBuilder.driverClassName("org.postgresql.Driver");
        dataSourceBuilder.url("jdbc:postgresql://localhost:5432/geo_test");
        dataSourceBuilder.username("postgres");
        dataSourceBuilder.password("root");
        return dataSourceBuilder.build();
    }

    @Bean
    public LocalSessionFactoryBean sessionFactory() {
        LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
        sessionFactory.setDataSource(dataSource());
        sessionFactory.setPackagesToScan("com.test.hibernate.domain");
        sessionFactory.setHibernateProperties(hibernateProperties());
        return sessionFactory;
    }

    @Bean
    public HibernateTransactionManager getTransactionManager() {
        HibernateTransactionManager transactionManager = new HibernateTransactionManager();
        transactionManager.setSessionFactory(sessionFactory().getObject());
        return transactionManager;
    }

    private Properties hibernateProperties() {
        final Properties properties = new Properties();
        properties.put(AvailableSettings.DIALECT, env.getRequiredProperty("hibernate.dialect"));
        properties.put(AvailableSettings.SHOW_SQL, env.getRequiredProperty("hibernate.show_sql"));
        properties.put(AvailableSettings.POOL_SIZE, env.getRequiredProperty("hibernate.connection_pool_size"));
        properties.put("hibernate.search.default_backend", env.getRequiredProperty("hibernate.search.default_backend"));
        properties.put("hibernate.search.backends.myBackend.type", env.getRequiredProperty("hibernate.search.backends.myBackend.type"));
        properties.put(AvailableSettings.HBM2DDL_AUTO, env.getRequiredProperty("hibernate.hbm2ddl.auto"));
        return properties;
    }
}

这是我的 hibernate.properties:

hibernate.dialect=org.hibernate.dialect.PostgresPlusDialect
hibernate.search.default_backend=myBackend
hibernate.search.backends.myBackend.type=lucene
hibernate.show_sql=true
hibernate.connection_pool_size=1
hibernate.hbm2ddl.auto=validate

以及位于 resources/db/migration 中的迁移文件:

create sequence hibernate_sequence start 1 increment 1;

create table geo_table (
    point_id int8 not null,
    latitude float8 not null,
    longitude float8 not null,

    primary key (point_id)
);

所以,现在我有下一个错误:

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'searchServiceImpl': Unsatisfied dependency expressed through field 'sessionManager'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'sessionManager': Unsatisfied dependency expressed through field 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [com/test/hibernate/config/HibernateConfig.class]: Invocation of init method failed; nested exception is org.hibernate.tool.schema.spi.SchemaManagementException: Schema-validation: missing table [geo_table]

更新: application.properties:

spring.flyway.url=jdbc:postgresql://localhost:5432/geo_test
spring.flyway.schemas=public
spring.flyway.user=postgres
spring.flyway.password=root
spring.flyway.baseline-version=1
spring.flyway.locations=classpath:db/migration
spring.flyway.enabled=true

有什么问题?我应该在 application.properties 中配置数据源吗?或者如何解决?

【问题讨论】:

标签: hibernate spring-boot flyway


【解决方案1】:

Flyway 期望有一个现有的数据库供您运行迁移。尝试创建数据库,添加数据源 url、用户名和密码。然后重新运行您的应用程序,将为您创建 geo_table。

【讨论】:

  • 我很困惑。您的问题是“为什么 flyway 没有创建数据库?”现在您的评论说“数据库已经创建”。是哪一个?
  • 对不起,我的意思是flyway没有创建表)
  • 在您的应用程序日志中,在Hibernate 日志开始之前,您是否有来自DbMigrate 的日志?我不熟悉 Gradle,所以我很难判断您是否正确配置了该 flyway 依赖项。
  • 你不需要那个 HibernateConfig 配置类。您应该能够让 Spring Boot 完成所有这些初始化。如果您的应用程序有@SpringBootApplication,那么它将自动扫描这些实体文件。
猜你喜欢
  • 2021-05-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-12
  • 2018-09-04
  • 2019-12-10
  • 2021-02-21
  • 2016-02-22
相关资源
最近更新 更多