如果 schema 不存在,则使用 spring Jpa 和 hibernate 来创建 schema

Dea*_*ool 5 java postgresql hibernate spring-data-jpa spring-boot

我正在开发 spring boot 2 应用程序,并尝试通过配置 hikari 数据源和 spring Jpa 与 postgresql 数据库建立连接。

我在这方面取得了成功,并且我正在使用hibernate.hbm2ddl.autoas update,因此它会在不存在的情况下创建表,但唯一的问题是当架构不存在时它会引发异常

错误

GenerationTarget encountered exception accepting command : Error executing DDL via JDBC Statement
Caused by: org.postgresql.util.PSQLException: ERROR: schema "test_schema" does not exist
Run Code Online (Sandbox Code Playgroud)

我通过配置类手动配置了一切

配置类

@Bean
@Primary
public HikariDataSource dataSource() {

    HikariConfig config = new HikariConfig();

    config.setJdbcUrl(databaseUrl);
    config.setUsername(username);
    config.setPassword(password);
    config.setDriverClassName(driverClassName);
    config.setConnectionTimeout(connectionTimeout);
    config.setIdleTimeout(idleTimeout);
    config.setMaximumPoolSize(maxpoolSize);
    config.setMaxLifetime(maxLifeTime);
    config.setMinimumIdle(minIdleConnections);
    //config.setPoolName(poolName);

    return new HikariDataSource(config);
}

@Bean
public Properties additionalProps() {
    Properties jpaProps = new Properties();

    jpaProps.put(hbDialect, "org.hibernate.dialect.PostgreSQLDialect");
    jpaProps.put(autoDDL, "update");
    jpaProps.put(showSql, true);
    jpaProps.put(formatSql, true);

    return jpaProps;
}

@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
    LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
    factory.setDataSource(dataSource());
    factory.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
    factory.setPackagesToScan("com.target.storetaskcount.entity");
    factory.setJpaProperties(additionalProps());
    return factory;
}

@Bean
public JpaTransactionManager transactionManager(EntityManagerFactory factory) {
    JpaTransactionManager transactionManager = new JpaTransactionManager();
    transactionManager.setEntityManagerFactory(entityManagerFactory().getObject());
    return transactionManager;
}
Run Code Online (Sandbox Code Playgroud)

如果不存在,我将如何创建模式?

我在这里看到了一个类似的问题,并尝试了一切,到目前为止没有运气类似的问题(我不想使用 Flyway db)

这是文档hibernate-doc,但不清楚如何添加此属性来创建模式javax.persistence.schema-generation.database.action

Dea*_*ool 5

添加此属性可以正常工作并创建模式(如果此处不存在)

jpaProps.put("javax.persistence.create-database-schemas", true);
Run Code Online (Sandbox Code Playgroud)

hibernate.hbm2ddl.create_namespaces 的 JPA 变体。指定持久性提供程序除了创建数据库对象(表、序列、约束等)之外是否还要创建数据库模式。如果持久性提供程序要在数据库中创建架构或生成包含“CREATE SCHEMA”命令的 DDL,则应将此布尔属性的值设置为 true。如果未提供此属性(或明确为 false),则提供程序不应尝试创建数据库模式。


小智 5

虽然其他答案完全正确,但我正在分享对我有用的内容。如果不存在,将其添加到application.properties文件中将创建模式:

spring.jpa.properties.hibernate.hbm2ddl.create_namespaces=true
Run Code Online (Sandbox Code Playgroud)

这里我们使用带有 Spring JPA 前缀的 Hibernate 本机属性 (spring.jpa.properties.*)。

同样,您可以通过这种方式使用许多其他Hibernate本机属性。

春季文档

您可以使用 spring.jpa.properties.* 来设置它以及其他 Hibernate 本机属性(在将它们添加到实体管理器之前会删除前缀)。

就我而言,我使用的是 MSSQL Server

  • 仅供参考:“hibernate.hbm2dll.create_namespaces”现已弃用。最好使用“hibernate.hbm2ddl.create_namespaces” (2认同)