当我设置 hibernate.hbm2ddl.auto 值来创建时,我没有收到任何错误, 但是当我将其设置为更新时,出现错误。 我需要创建表并更新它们,所以我需要更新值。 有什么可能出错的想法吗?
这里是错误:
15:55:19,148 ERROR [org.jboss.as.controller.management-operation] (Controller Boot Thread) WFLYCTL0013: Operation ("deploy") failed - address: ([("deployment" => "WebService.war")]) - failure description: {"WFLYCTL0080: Failed services" => {"jboss.persistenceunit.\"WebService.war#swap\"" => "javax.persistence.PersistenceException: [PersistenceUnit: swap] Unable to build Hibernate SessionFactory Caused by: javax.persistence.PersistenceException: [PersistenceUnit: swap] Unable to build Hibernate SessionFactory Caused by: org.hibernate.exception.SQLGrammarException: Unable to build DatabaseInformation Caused by: org.h2.jdbc.JdbcSQLException: Table \"PG_CLASS\" not found; SQL statement: select relname from pg_class where relkind='S' [42102-193]"}} 15:55:19,154 INFO [org.jboss.as.server] (ServerService Thread Pool -- 37) WFLYSRV0010: Deployed "WebService.war" (runtime-name : "WebService.war") 15:55:19,155 INFO [org.jboss.as.server] (ServerService Thread Pool -- 37) WFLYSRV0010: Deployed "test.war" (runtime-name : "test.war") 15:55:19,155 INFO [org.jboss.as.controller] (Controller Boot Thread) WFLYCTL0183: Service status report WFLYCTL0186: Services which failed to start: service jboss.persistenceunit."WebService.war#swap": javax.persistence.PersistenceException: [PersistenceUnit: swap] Unable to build Hibernate SessionFactory
这是我的持久性 xml 文件:
<?xml version="1.0" encoding="UTF-8"?>
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"> org.hibernate.jpa.HibernatePersistenceProvider
<class>org.Swap.WebService.Model.User</class>
<class>org.Swap.WebService.Model.BaseEntity</class>
<properties>
<!-- Hibernate properties -->
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="hibernate.connection.zeroDateTimeBehavior" value="convertToNull" />
<property name="hibernate.show_sql" value="false" />
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>
<!-- Database properties -->
<property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver" /> <!-- DB Driver -->
<property name="javax.persistence.jdbc.url" value="jdbc:postgresql://127.0.0.1:5432/swap" /> <!-- BD Mane -->
<property name="javax.persistence.jdbc.user" value="hidden" /> <!-- DB User -->
<property name="javax.persistence.jdbc.password" value="hidden" /> <!-- DB Password -->
</properties>
</persistence-unit>
编辑: 如果我用
开始野蝇<property name="hibernate.hbm2ddl.auto" value="create"/>
然后将其设置为更新和发布它将一直有效,直到我重新启动 Wildfly。 会不会是wildfly加载的问题?
编辑2: 这是来 self 的独立 xml 的数据源:
<datasources>
<datasource jndi-name="java:jboss/datasources/ExampleDS" pool-name="ExampleDS" enabled="true" use-java-context="true">
<connection-url>jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE</connection-url>
<driver>h2</driver>
<security>
<user-name>sa</user-name>
<password>sa</password>
</security>
</datasource>
<datasource jta="false" jndi-name="java:jboss/datasources/swap" pool-name="swap" enabled="true" use-java-context="true">
<connection-url>jdbc:postgresql://127.0.0.1:5432/swap?useUnicode=yes&characterEncoding=UTF-8</connection-url>
<driver>org.postgresql</driver>
<security>
<user-name>postgres</user-name>
<password>postgres</password>
</security>
</datasource>
<drivers>
<driver name="h2" module="com.h2database.h2">
<xa-datasource-class>org.h2.jdbcx.JdbcDataSource</xa-datasource-class>
</driver>
<driver name="org.postgresql" module="org.postgresql">
<driver-class>org.postgresql.Driver</driver-class>
<xa-datasource-class>org.postgresql.Driver</xa-datasource-class>
</driver>
</drivers>
</datasources>
最佳答案
问题是关于方言和驱动程序集。
您的 hibernate 使用 H2 驱动程序和 Postgres 方言。
Caused by: org.h2.jdbc.JdbcSQLException:
您可以在 hibernate 日志中看到它使用的驱动程序和方言。 这是显示 hibernate 日志应该是什么样子的片段:
INFO [Version] - HHH000412: Hibernate Core {4.3.7.Final}
INFO [Environment] - HHH000206: hibernate.properties not found
INFO [Environment] - HHH000021: Bytecode provider name : javassist
INFO [MppNamingStrategy] - using naming strategy: MppNamingStrategy
INFO [Version] - HCANN000001: Hibernate Commons Annotations {4.0.5.Final}
DEBUG [JdbcServicesImpl] - Driver ->
name : H2 JDBC Driver
version : 1.4.196 (2017-06-10)
major : 1
minor : 4
DEBUG [JdbcServicesImpl] - JDBC version : 4.0
INFO [Dialect] - HHH000400: Using dialect: org.hibernate.dialect.H2Dialect
INFO [ASTQueryTranslatorFactory] - HHH000397: Using ASTQueryTranslatorFactory
INFO [Version] - HV000001: Hibernate Validator 4.3.2.Final
INFO [SchemaValidator] - HHH000229: Running schema validator
在这个例子中可以看到hibernate使用
- H2 JDBC 驱动程序
- org.hibernate.dialect.H2Dialect
在您的堆栈跟踪中,我们可以看到您的 hibernate 使用 postgres 方言(好),但使用 H2 驱动程序(坏)
关于java - hbm2ddl.auto 设置为更新时 hibernate 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53351943/