0

I'm new to JPA, Hibernate and Maven and I'm using this video tutorial to create a project. I'm running into some problems with Hibernate, I believe, but the other answers I've seen on this website don't seem to address my problem. I'm using Hibernate 5.4.1.Final and mysql 8.0.20. Below is the partial stack trace (full is pretty long).

WARN: HHH000342: Could not obtain connection to query metadata : null
Exception in thread "main" java.lang.ExceptionInInitializerError
Caused by: 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:275)
...
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:79)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:54)
    at com.sommaven.JEETut3.TestSystem.<clinit>(TestSystem.java:15)
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:101)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:263)
...

This is my persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
  
<persistence version="2.0"
    xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
    
    <!-- Define a name used to get an entity manager. Define that you will 
    complete transactions with the DB  -->
    <persistence-unit name="JEETut3" transaction-type="RESOURCE_LOCAL">
    
        <!-- Define the class for Hibernate which implements JPA -->
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <!-- Define the object that should be persisted in the database -->
        <class>com.newthinktank.JEETut3.Customer</class>
        <properties>
            <!-- Driver for DB database -->
            <property name="javax.persistence.jdbc.driver" value="com.mysql.cj.jdbc.Driver" />
            <!-- URL for DB -->
            <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost/test4" />
            <!-- Username -->
            <property name="javax.persistence.jdbc.user" value="dbadmin" />
            <!-- Password -->
            <property name="javax.persistence.jdbc.password" value="turtledove" />
        </properties>
    </persistence-unit>
</persistence>

And this is my pom.xml

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  
  <!-- 1. New -> Maven Project (Quick Start)
  Group Id : com.newthinktank
  Artifact ID : JEETut3
  Update the dependencies here
  Maven is project management software that handles builds, dependencies, documentation and more
   -->
 
  <groupId>com.newthinktank</groupId>
  <artifactId>JEETut3</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>
 
  <name>JEETut3</name>
  <url>http://maven.apache.org</url>
 
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
 
  <dependencies>
  
  <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>5.4.1.Final</version>
    </dependency>
 
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>5.4.1.Final</version>
    </dependency>
 
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.15</version>
        <scope>provided</scope>
    </dependency>
  
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

I see that the hibernate.dialect isn't set, do I need to manually set it to mysql?

  • Similar problem asked here https://stackoverflow.com/questions/55470977/hhh000342-could-not-obtain-connection-to-query-metadata-null, you can check it – Ravindra Jun 23 '20 at 04:50

1 Answers1

0

You are seeing this exception as you haven't provided any dialect in the persistence.xml and left it to Hibernate to resolve automatically at startup. Hibernate can determine the correct dialect to use automatically, but in order to do this, it needs a live connection to the database. I think the dialect is not getting resolved for you at startup due to connectivity issues maybe, so try adding the following to your persistence.xml:

    <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLInnoDBDialect" />

You could also check to verify that the database server that you are trying to connect to is up and running or the driver versions are compatible or not. Also double check the url , password etc to verify if you left out any particular port that you need to specify etc.

Ananthapadmanabhan
  • 4,089
  • 5
  • 17
  • 31