如果不存在,则使用 spring Jpa 和 hibernate 创建模式 [英] Create schema if does not exist by using spring Jpa with hibernate

查看:26
本文介绍了如果不存在,则使用 spring Jpa 和 hibernate 创建模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我成功了,我使用 hibernate.hbm2ddl.auto 作为 update 所以它正在创建表,如果不存在,但唯一的问题是它当架构不存在时抛出异常

错误

GenerationTarget 遇到异常接受命令:通过 JDBC 语句执行 DDL 时出错引起:org.postgresql.util.PSQLException:错误:模式test_schema"不存在

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

配置类

@Bean@基本的公共 HikariDataSource 数据源(){HikariConfig config = new HikariConfig();config.setJdbcUrl(databaseUrl);config.setUsername(用户名);config.setPassword(密码);config.setDriverClassName(driverClassName);config.setConnectionTimeout(connectionTimeout);config.setIdleTimeout(idleTimeout);config.setMaximumPoolSize(maxpoolSize);config.setMaxLifetime(maxLifeTime);config.setMinimumIdle(minIdleConnections);//config.setPoolName(poolName);返回新的 HikariDataSource(config);}@豆角,扁豆公共属性附加道具(){属性 jpaProps = new Properties();jpaProps.put(hbDialect, "org.hibernate.dialect.PostgreSQLDialect");jpaProps.put(autoDDL, "更新");jpaProps.put(showSql, true);jpaProps.put(formatSql, true);返回 jpaProps;}@豆角,扁豆public LocalContainerEntityManagerFactoryBean entityManagerFactory() {LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();factory.setDataSource(dataSource());factory.setJpaVendorAdapter(new HibernateJpaVendorAdapter());factory.setPackagesToScan("com.target.storetaskcount.entity");factory.setJpaProperties(additionalProps());返厂;}@豆角,扁豆公共 JpaTransactionManager 事务管理器(EntityManagerFactory 工厂){JpaTransactionManager transactionManager = new JpaTransactionManager();transactionManager.setEntityManagerFactory(entityManagerFactory().getObject());返回事务管理器;}

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

我在这里看到了一个类似的问题并且尝试了一切都没有成功类似-问题(我不想使用flyway db)

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

解决方案

如果不存在,则添加此属性有效并创建模式 这里

jpaProps.put("javax.persistence.create-database-schemas", true);

<块引用>

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

I'm working on spring boot 2 application and trying to establish connection with postgresql database with configuring hikari datasource and spring Jpa.

I'm succeed in that and i'm using hibernate.hbm2ddl.auto as update so it is creating table if not exists, but only the problem is it is throwing an exception when schema does not exist

Error

GenerationTarget encountered exception accepting command : Error executing DDL via JDBC Statement
Caused by: org.postgresql.util.PSQLException: ERROR: schema "test_schema" does not exist

I configured everything manually through config class

Config Class

@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;
}

How would i make this to create schema if not exist?

I saw a similar question here and tried everything no luck so far similar-question (i don't wanna use flyway db)

Here is the documentation hibernate-doc, but it's not clear how add this property to create schema javax.persistence.schema-generation.database.action

解决方案

Adding this property worked and created schema if not exist here

jpaProps.put("javax.persistence.create-database-schemas", true);

The JPA variant of hibernate.hbm2dll.create_namespaces. Specifies whether the persistence provider is to create the database schema(s) in addition to creating database objects (tables, sequences, constraints, etc). The value of this boolean property should be set to true if the persistence provider is to create schemas in the database or to generate DDL that contains "CREATE SCHEMA" commands. If this property is not supplied (or is explicitly false), the provider should not attempt to create database schemas.

这篇关于如果不存在,则使用 spring Jpa 和 hibernate 创建模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆