将 Hibernate 与 OpenLiberty 结合使用时获取 'hibernate.dialect' 未设置

Getting 'hibernate.dialect' not set when using Hibernate with OpenLiberty

当我尝试将 open liberty 20 与 hibernate 集成时,它给我留下了以下堆栈跟踪:

[WARNING ] HHH000342: Could not obtain connection to query metadata : Unable to determine Dialect to use [name=Generic Component Context DataSource, majorVersion=0]; user must register resolver or explicitly set 'hibernate.dialect'
[ERROR   ] CWWJP0015E: An error occurred in the org.hibernate.jpa.HibernatePersistenceProvider persistence provider when it attempted to create the container entity manager factory for the model-unit persistence unit. The following error occurred: org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:244)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:208)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:189)
    at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:51)
    at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:94)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:217)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:189)
    at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.handleTypes(MetadataBuildingProcess.java:352)
    at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:111)
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata(EntityManagerFactoryBuilderImpl.java:848)
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:876)
    at org.hibernate.jpa.HibernatePersistenceProvider.createContainerEntityManagerFactory(HibernatePersistenceProvider.java:135)
    at com.ibm.ws.jpa.management.JPAPUnitInfo.createEMFactory(JPAPUnitInfo.java:919)
    at [internal classes]
Caused by: org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set
    at org.hibernate.engine.jdbc.dialect.internal.DialectFactoryImpl.determineDialect(DialectFactoryImpl.java:100)
    at org.hibernate.engine.jdbc.dialect.internal.DialectFactoryImpl.buildDialect(DialectFactoryImpl.java:54)
    at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:137)
    at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:35)
    at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.initiateService(StandardServiceRegistryImpl.java:88)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:234)
    ... 13 more

我的 persistence.xml 在 META-INF 文件夹中,如下

<?xml version="1.0" encoding="UTF-8"?>
<persistence xsi:schemaLocation="http://java.sun.com/xml/ns/persistence 
                                http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd" 
             version="2.0" 
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
             xmlns="http://java.sun.com/xml/ns/persistence">
 
   <persistence-unit name="jpa-unit" transaction-type="JTA">
      <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
      <jta-data-source>jdbc/eventjpadatasource</jta-data-source>
      <properties>
         <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQL9Dialect"/>
      </properties>
   </persistence-unit>
</persistence>

我的 server.xml 包含我的所有数据库配置,如下所示:

<server description="new server">

    <!-- Enable features -->
    <featureManager>
        <feature>webProfile-8.0</feature>
        <feature>localConnector-1.0</feature>
    </featureManager>

    <!-- To access this server from a remote client add a host attribute to the following element, e.g. host="*" -->
    <httpEndpoint httpPort="9080" httpsPort="9443" id="defaultHttpEndpoint"/>
                  
    <!-- Automatically expand WAR files and EAR files -->
    <applicationManager autoExpand="true"/>

    <!-- Default SSL configuration enables trust for default certificates from the Java runtime --> 
    <ssl id="defaultSSLConfig" trustDefaultCerts="true"/>
<dataSource id="eventjpadatasource" jndiName="jdbc/eventjpadatasource" type="javax.sql.XADataSource">
      <jdbcDriver javax.sql.XADataSource="org.postgresql.xa.PGXADataSource" libraryRef="postgresql-library">
        </jdbcDriver>
      <properties databaseName="db_anme" password="12345" portNumber="5432" serverName="127.0.0.1" user="postgres"/>
    </dataSource>
    
    <library id="postgresql-library">
      <fileset dir="${shared.resource.dir}" includes="postgresql*.jar"/>
  </library>

    <applicationMonitor updateTrigger="mbean"/>

    <webApplication id="CategoriesMicroservice" location="CategoriesMicroservice-1.0.war" name="CategoriesMicroservice"/>
</server>

如您所见,hibernate.dialect 就在那里。

My persistence.xml is inside META-INF folder

CategoriesMicroservice-1.0.war/META-INF/ ?

JPA 规范;第 8.2 节:

持久性单元由 persistence.xml 文件定义。其 META-INF 目录包含 persistence.xml 文件的 jar 文件或目录称为持久性单元的根目录。在 Java EE 环境中,持久性单元的根必须是以下之一:

  • 一个 EJB-JAR 文件
  • WAR 文件的 WEB-INF/classes 目录[87]
  • WAR 文件WEB-INF/lib 目录中的一个 jar 文件
  • EAR 库目录中的一个 jar 文件
  • 一个应用程序客户端 jar 文件

如果 persistence.xml 打包不正确,您应该会收到有关缺少对 jpa-unit.

的持久性单元引用的异常