Search code examples
hibernatejpaopen-liberty

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


When I am trying to integrate open liberty 20 with hibernate it is dropping me the following stacktrace:

[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

My persistence.xml is inside META-INF folder and it is the following

<?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>

My server.xml contains all mine db configurations and it follows:

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

As you can see the hibernate.dialect is there.


Solution

  • My persistence.xml is inside META-INF folder

    CategoriesMicroservice-1.0.war/META-INF/ ?

    JPA Spec; section 8.2:

    A persistence unit is defined by a persistence.xml file. The jar file or directory whose META-INF directory contains the persistence.xml file is termed the root of the persistence unit. In Java EE environments, the root of a persistence unit must be one of the following:

    • an EJB-JAR file
    • the WEB-INF/classes directory of a WAR file[87]
    • a jar file in the WEB-INF/lib directory of a WAR file
    • a jar file in the EAR library directory
    • an application client jar file

    If the persistence.xml was packaged incorrectly though, you should be getting exceptions concerning missing persistence-unit references to jpa-unit.