猿问

如果不存在,则通过将 spring Jpa 与 hibernate 一起使用来创建模式

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


我成功了,我正在使用它,如果不存在hibernate.hbm2ddl.auto,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

我通过配置类手动配置了所有内容


配置类


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

}


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


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


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


慕妹3242003
浏览 168回答 3
3回答

宝慕林4294392

如果此处不存在,则添加此属性有效并创建架构jpaProps.put("javax.persistence.create-database-schemas", true);hibernate.hbm2dll.create_namespaces 的 JPA 变体。指定除了创建数据库对象(表、序列、约束等)之外,持久性提供程序是否还要创建数据库模式。如果持久性提供程序要在数据库中创建模式或生成包含“CREATE SCHEMA”命令的 DDL,则该布尔属性的值应设置为 true。如果未提供此属性(或明确为 false),则提供者不应尝试创建数据库模式。

斯蒂芬大帝

虽然另一个答案是完全正确的,但我正在分享对我有用的东西。如果不存在,将其添加到application.properties文件将创建模式spring.jpa.properties.hibernate.hbm2dll.create_namespaces=true这里我们使用带有 Spring JPA 的 Hibernates 原生属性 prefixed(spring.jpa.properties.*)。同样,您可以通过这种方式使用许多其他 Hibernates 原生属性。春季文档您可以使用 spring.jpa.properties.* 设置它以及其他 Hibernate 本机属性(在将它们添加到实体管理器之前去除前缀)。*就我而言,我使用的是 MSSQL Server

慕田峪7331174

这对我在 Springboot 2.6.2 上的 Postgres 有用。如果它不存在,它将创建模式(适配器边界),并在创建所有实体表时将其用作 default_schema。为记录启用 TRACE 以查看它的工作原理。spring:  jpa:    open-in-view: false    generate-ddl: true    properties:      javax:        persistence:          schema-generation:            database:              action: create      hibernate:        show_sql: true        use_sql_comments: true        format_sql: true        generate_statistics: false        jdbc:          fetch_size: 2000          lob.non_contextual_creation: true        dialect: org.hibernate.dialect.PostgreSQLDialect        ddl-auto: create        hbm2dll:          create_namespaces: true        default_schema: adapterborder  datasource:    url: jdbc:postgresql://localhost:5432/postgres?currentSchema=adapterborder    username: postgres    password: postgres    driver-class-name: org.postgresql.Driver    testWhileIdle: true    hikari:      minimumIdle: 5      maximumPoolSize: 20      idleTimeout: 30000      poolName: SpringBootJPAHikariCP      maxLifetime: 200000      connectionTimeout: 30000      connection-test-query: SELECT 1logging:  level:    '[org.springframework.data]': TRACE    '[org.hibernate]': TRACE    '[javax.persistence]': TRACE
随时随地看视频慕课网APP

相关分类

Java
我要回答