[spring-projects/spring-boot]Spring 3.xx 不创建模式

2024-05-08 17 views
0

如果在数据库中找不到(使用 postgresql),Spring 2.7.2 会创建模式。我所做的就是在实体中指定 schema ="foo" 并在配置文件中指定 spring.jpa.properties.hibernate.hbm2dll.create_namespaces=true ,当然还有这些 spring.jpa.hibernate.ddl-auto= 更新 spring.jpa .generate-ddl=true

我在 spring 3.1.0 上启动了一个新项目,并使用上面的设置未创建模式。迫使我从数据库手动创建架构。

此更改是故意的还是配置设置已更改?

回答

9

架构创建使用spring.jpa.hibernate.ddl-auto是 Hibernate 功能,应该继续与 Spring Boot 3.1.x 一起使用。听起来您的情况并非如此,但从您到目前为止的描述中我无法说出原因。如果您希望我们花更多时间进行调查,请花一些时间提供重现问题的完整但最小的示例。您可以通过将其推送到 GitHub 上的单独存储库或将其压缩并附加到此问题来与我们共享。

4

感谢您提供样品。不幸的是,它不能重现该问题。我启动应用程序时,它会更新架构以为应用程序的一个实体创建表:

2023-06-20T08:27:48.308+01:00 DEBUG 62931 --- [  restartedMain] org.hibernate.SQL                        : 
    create table paxi.roles (
        id serial not null,
        name varchar(20),
        primary key (id)
    )
Hibernate: 
    create table paxi.roles (
        id serial not null,
        name varchar(20),
        primary key (id)
    )

为了进一步调查,我们需要一个重现该问题的样本。

0

感谢您的答复。是的,它输出了,但表不是在 postgresql 数据库中创建的。

如果您检查,您将找不到它们。奇怪的是它没有抛出错误......我认为这是一个错误

屏幕截图 2023-06-20 12 17 06 PM
7

如图所示psql,该表已按照架构中的预期创建paxi

paxi=# set SEARCH_PATH to paxi;
SET
paxi=# \dt
          List of relations
 Schema | Name  | Type  |   Owner    
--------+-------+-------+------------
 paxi   | roles | table | foodfinder
(1 row)

paxi查看您的屏幕截图,您的数据库中没有架构。

如果我paxi从数据库中删除架构并重新启动应用程序,Postgres 会记录以下内容:

2023-06-20 12:08:09.766 UTC [106] ERROR:  schema "paxi" does not exist at character 19
2023-06-20 12:08:09.766 UTC [106] STATEMENT:  
        create table paxi.roles (
            id serial not null,
            name varchar(20),
            primary key (id)
        )

当您将 Hibernate 的日志记录调整为仅显示错误级别日志记录时,此问题会悄然发生:

logging.level.org.hibernate: ERROR

如果我删除它并再次重新启动应用程序,则会记录以下内容:

2023-06-20T13:10:35.488+01:00  WARN 87590 --- [  restartedMain] o.h.t.s.i.ExceptionHandlerLoggedImpl     : GenerationTarget encountered exception accepting command : Error executing DDL "
    create table paxi.roles (
        id serial not null,
        name varchar(20),
        primary key (id)
    )" via JDBC [ERROR: schema "paxi" does not exist
  Position: 19]

org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL "
    create table paxi.roles (
        id serial not null,
        name varchar(20),
        primary key (id)
    )" via JDBC [ERROR: schema "paxi" does not exist
  Position: 19]
    at org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase.accept(GenerationTargetToDatabase.java:92) ~[hibernate-core-6.2.2.Final.jar:6.2.2.Final]
    at org.hibernate.tool.schema.internal.AbstractSchemaMigrator.applySqlString(AbstractSchemaMigrator.java:574) ~[hibernate-core-6.2.2.Final.jar:6.2.2.Final]
    at org.hibernate.tool.schema.internal.AbstractSchemaMigrator.applySqlStrings(AbstractSchemaMigrator.java:514) ~[hibernate-core-6.2.2.Final.jar:6.2.2.Final]
    at org.hibernate.tool.schema.internal.AbstractSchemaMigrator.createTable(AbstractSchemaMigrator.java:315) ~[hibernate-core-6.2.2.Final.jar:6.2.2.Final]
    at org.hibernate.tool.schema.internal.GroupedSchemaMigratorImpl.performTablesMigration(GroupedSchemaMigratorImpl.java:80) ~[hibernate-core-6.2.2.Final.jar:6.2.2.Final]
    at org.hibernate.tool.schema.internal.AbstractSchemaMigrator.performMigration(AbstractSchemaMigrator.java:232) ~[hibernate-core-6.2.2.Final.jar:6.2.2.Final]
    at org.hibernate.tool.schema.internal.AbstractSchemaMigrator.doMigration(AbstractSchemaMigrator.java:117) ~[hibernate-core-6.2.2.Final.jar:6.2.2.Final]
    at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.performDatabaseAction(SchemaManagementToolCoordinator.java:284) ~[hibernate-core-6.2.2.Final.jar:6.2.2.Final]
    at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.lambda$process$5(SchemaManagementToolCoordinator.java:143) ~[hibernate-core-6.2.2.Final.jar:6.2.2.Final]
    at java.base/java.util.HashMap.forEach(HashMap.java:1421) ~[na:na]
    at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.process(SchemaManagementToolCoordinator.java:140) ~[hibernate-core-6.2.2.Final.jar:6.2.2.Final]
    at org.hibernate.boot.internal.SessionFactoryObserverForSchemaExport.sessionFactoryCreated(SessionFactoryObserverForSchemaExport.java:37) ~[hibernate-core-6.2.2.Final.jar:6.2.2.Final]
    at org.hibernate.internal.SessionFactoryObserverChain.sessionFactoryCreated(SessionFactoryObserverChain.java:35) ~[hibernate-core-6.2.2.Final.jar:6.2.2.Final]
    at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:291) ~[hibernate-core-6.2.2.Final.jar:6.2.2.Final]
    at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:431) ~[hibernate-core-6.2.2.Final.jar:6.2.2.Final]
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:1455) ~[hibernate-core-6.2.2.Final.jar:6.2.2.Final]
    at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:66) ~[spring-orm-6.0.9.jar:6.0.9]
    at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:376) ~[spring-orm-6.0.9.jar:6.0.9]
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:409) ~[spring-orm-6.0.9.jar:6.0.9]
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:396) ~[spring-orm-6.0.9.jar:6.0.9]
    at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.afterPropertiesSet(LocalContainerEntityManagerFactoryBean.java:352) ~[spring-orm-6.0.9.jar:6.0.9]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1816) ~[spring-beans-6.0.9.jar:6.0.9]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1766) ~[spring-beans-6.0.9.jar:6.0.9]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:598) ~[spring-beans-6.0.9.jar:6.0.9]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:520) ~[spring-beans-6.0.9.jar:6.0.9]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:326) ~[spring-beans-6.0.9.jar:6.0.9]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-6.0.9.jar:6.0.9]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:324) ~[spring-beans-6.0.9.jar:6.0.9]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200) ~[spring-beans-6.0.9.jar:6.0.9]
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1156) ~[spring-context-6.0.9.jar:6.0.9]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:931) ~[spring-context-6.0.9.jar:6.0.9]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:608) ~[spring-context-6.0.9.jar:6.0.9]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:146) ~[spring-boot-3.1.0.jar:3.1.0]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:733) ~[spring-boot-3.1.0.jar:3.1.0]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:435) ~[spring-boot-3.1.0.jar:3.1.0]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:311) ~[spring-boot-3.1.0.jar:3.1.0]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1305) ~[spring-boot-3.1.0.jar:3.1.0]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1294) ~[spring-boot-3.1.0.jar:3.1.0]
    at com.example.demo.DemoApplication.main(DemoApplication.java:11) ~[classes/:na]
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) ~[na:na]
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
    at java.base/java.lang.reflect.Method.invoke(Method.java:568) ~[na:na]
    at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) ~[spring-boot-devtools-3.1.0.jar:3.1.0]
Caused by: org.postgresql.util.PSQLException: ERROR: schema "paxi" does not exist
  Position: 19
    at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2713) ~[postgresql-42.6.0.jar:42.6.0]
    at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2401) ~[postgresql-42.6.0.jar:42.6.0]
    at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:368) ~[postgresql-42.6.0.jar:42.6.0]
    at org.postgresql.jdbc.PgStatement.executeInternal(PgStatement.java:498) ~[postgresql-42.6.0.jar:42.6.0]
    at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:415) ~[postgresql-42.6.0.jar:42.6.0]
    at org.postgresql.jdbc.PgStatement.executeWithFlags(PgStatement.java:335) ~[postgresql-42.6.0.jar:42.6.0]
    at org.postgresql.jdbc.PgStatement.executeCachedSql(PgStatement.java:321) ~[postgresql-42.6.0.jar:42.6.0]
    at org.postgresql.jdbc.PgStatement.executeWithFlags(PgStatement.java:297) ~[postgresql-42.6.0.jar:42.6.0]
    at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:292) ~[postgresql-42.6.0.jar:42.6.0]
    at com.zaxxer.hikari.pool.ProxyStatement.execute(ProxyStatement.java:94) ~[HikariCP-5.0.1.jar:na]
    at com.zaxxer.hikari.pool.HikariProxyStatement.execute(HikariProxyStatement.java) ~[HikariCP-5.0.1.jar:na]
    at org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase.accept(GenerationTargetToDatabase.java:78) ~[hibernate-core-6.2.2.Final.jar:6.2.2.Final]
    ... 43 common frames omitted
5

该架构不是自动创建的,因为属性名称中有拼写错误。应该是spring.jpa.properties.hibernate.hbm2ddl.create_namespaces=true,但你有spring.jpa.properties.hibernate.hbm2dll.create_namespaces=truehbm2ddl而不是hbm2dll)。

2

@wilkinsona 非常感谢您指出错误。我将其更改为 hbm2ddl 并且它可以工作,创建模式 paxi。奇怪的 hbm2dll 适用于以前的休眠版本。

再次感谢您的宝贵时间。

3

这完全概括了这一点。已经在这上面花了 48 小时了。我认为他们在 6.2.X 中删除了它。

3

@wilkinsona你好,我在这个问题上也需要一些帮助。实际上我已经创建了一个 spring boot 服务并尝试将其与 mysql 连接。程序运行没有错误,但 Hibernate 不会在数据库中创建表。

6

eclipse_z3wwfaCKGN

0

MySQLWorkbench_INbDmMU9Gh

4

@UURG 如果您需要 Hibernate 方面的帮助,请询问Hibernate 社区