skip to Main Content

I am writing a small program in Java with Spring boot. It is connecting to a MySQL Database that is dockerized. upon running the program, I get the following Error message

org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘entityManagerFactory’ defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Could not determine recommended JdbcType for org.workshop.spring.CourseApi.Course

Upon Googleing that, I found a YouTube video that basically tells you to verify your application.yml file as well as this stack overflow thread, with no marked solution.
Could not determine recommended JdbcType for <class>

As of now there are no relations between my entities and the class that’s causing the problem, according to the Error message is this one

package org.workshop.spring.CourseApi;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table
public class Course {
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Integer id;
    private String subject;
    private Coach responsibleCoach;
    private Student[] attendingStudents;

}

To be complete I’ll add the application.yml below

spring:
    datasource:
        username: root
        password: 123
        url: jdbc:mysql://localhost:6033/school_db
        driver-class-name: com.mysql.cj.jdbc.Driver
    jpa:
        generate-ddl: true
        hibernate:
            ddl:
                auto: true #allowes for creation and modification of the database schema
        show-sql: true # prints sql statements to console

I tried connecting to the database by hand, using the same data as I configured and that works, also if I shut down the Docker Containers, the connection fails, which is expected and makes me think the problem stems from the programming side.

I Also tried changing the MySQL connector in the pom.xml but the one used hereSpring boot guides repo seems to be outdated as the initializer provides the one I am currently using.

As requested here is the pom.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.0.5</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>org.workshop.spring</groupId>
    <artifactId>CourseApi</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>CourseApi</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>20</java.version>
    </properties>
<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.mysql</groupId>
            <artifactId>mysql-connector-j</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

And the docker-compose.yml:

version: '3'


networks:
    mysql-phpmyadmin:
        name: mysql-phpmyadmin
        driver: bridge

volumes:
    mysqldata:
        driver: local
        driver_opts:
            type: 'none'
            o: 'bind'
            device: '${HOME}/Documents/test/mysql-phpmyadmin/data'

services:
    mysql:
        image: mysql:latest
        container_name: mysql
        environment:
            MYSQL_ROOT_PASSWORD: 123
            MYSQL_DATABASE: shoolapi_db
            MYSQL_USER: user
            MYSQL_PASSWORD: 123
        ports:
        - "6033:3306"
        expose:
        - "3306"
        volumes:
            - mysqldata:/var/lib/mysql
        networks:
          mysql-phpmyadmin:
           aliases:
              - mysql


    phpmyadmin:
        image: phpmyadmin:latest
        container_name: phpmyadmin
        links:
            - mysql
        environment:
            PMA_HOST: mysql
            PMA_PORT: 3306
        ports:
            - 8081:80
        networks:
          mysql-phpmyadmin:
               aliases:
                 - phpmyadmin      

I am at my wits end and I hope one of you can provide me with some insight.
Thank you

2

Answers


  1. Chosen as BEST ANSWER

    Turns out the error came from not established relations between Entity's. Putting in all those along with the edits to the application.yml suggested above, solves the issue


  2. Could you start by changing your you SQL dependency to this

      <dependency>
          <groupId>mysql</groupId>
          <artifactId>mysql-connector-java</artifactId>
          <version>{version}</version> // you can try 8.0.16
      </dependency>
    

    For your docker-compose, the port mapping should work fine otherwise try to use the same port mapping

    Another problem is that your ddl-auto formatting and value, also add the dialect

      jpa:
        generate-ddl: true
        hibernate:
          ddl-auto: create // true is not an option
        show-sql: true
        database-platform: org.hibernate.dialect.MySQL5InnoDBDialect
    

    or for the dialect

    spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect
    

    When this run you would probably face an issue that school_db does not exist. You should create the schema first.

    if the same error persist, it could because of your spring boot and java version. You either need to down grade the version or use a higher version of mysql dependency.

    Looking at the version you are using, it seems that jpa imports moved from javax to jakarta in case you decided to downcast the version

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search