javamysqlspring-boothibernate

Spring Boot, JPA, and MySQL 8 Dialect Errors


I have a Spring Boot 2.4.5 application (latest version at this time). Spring Boot Data JPA has Hibernate 5.4.30.Final which is nearly the latest, and MySQL 8. This was an older version of Spring Boot which used Hibernate 5 and used Junit 4, and it was working correctly. I decided to update all my libraries to the latest libraries and everything broke. So, now I am putting my application back together with the latest libraries and running into a few issues. Most of those issues I was able to resolve, but this one hasn't been resolved yet, and that's why I came here.

I have the following configurations:

Maven MySQL Dependency:

    <groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.24</version>

The application.properties file has the following:

spring.datasource.url=jdbc:mysql://localhost:3306/phonebook
spring.datasource.username=username
spring.datasource.password=password
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.jpa.database-platform=org.hibernate.dialect
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=none
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect
spring.jpa.properties.hibernate.dialect.storage_engine=innodb
spring.data.jpa.repositories.enabled=true

My Configuration File looks like:

@Configuration
@ComponentScan(basePackages =
{ "com.tomholmes.springboot.phonebook.server" })
@PropertySource(value = "classpath:application.properties")
@EntityScan("com.tomholmes.springboot.phonebook.server.domain")  
@EnableJpaRepositories(basePackages = "com.tomholmes.springboot.phonebook.server.dao")
public class RepositoryContextConfiguration
{
    @Bean(name = "entityManagerFactory")
    public LocalSessionFactoryBean sessionFactory()
    {
        LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
        return sessionFactory;
    }
}

My Test Case looks like this below, and you should know that I am using the JUnit 5 jupiter.test classes.

@ExtendWith(SpringExtension.class)
@SpringBootTest(classes = RepositoryContextConfiguration.class)
@ComponentScan("com.tomholmes.springboot.phonebook.server")
@Transactional
public class BaseDaoTests
{
      @Autowired UserDao userDao;

      @Test
      public void testGetUserById()  {
            UserEntity userEntity = userDao.findById(1L);
      }
}

Whenever I run my unit test, I get this error:

Caused by: org.hibernate.HibernateException: 
     Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set

So obviously, I went to Google and I put this into the search. It came back with a lot of hits, but mostly from StackOverflow. So, I spent my morning pouring over the answers already posted here, and some other places on the internet as well. Some people solved their solution, but it was older versions of Hibernate, or Spring or Spring Boot, so none of those seemed to fit. I am hoping this is a small fix, and once I get past this log-jam, then I think the rest of my code will flow easier.

Thanks for any help, and if there is anything else you'd like me to post, let me know. Thanks again!

FYI: I have a Spring 5.3.6 Application with Hibernate 5.4.31.Final and JUnit 4. This application talks to the same database. Same tables, same localhost, same port, same user who has the correct positions, etc., so I know the database is running and the tables match the entities.


Solution

  • I finally figured this problem out. It wasn't so much the application.properties file, but I am going to share what I have now. I know it may seem like over-kill on some of this, and some properties are redundant. But, now that I have my DAO layer working, I can tweak the properties to get just what I need.

    application.properties

    spring.datasource.url=jdbc:mysql://localhost:3306/phonebook?serverTimezone=UTC
    spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
    spring.datasource.username=username
    spring.datasource.password=password
    spring.jpa.database=mysql
    spring.jpa.show-sql=true
    spring.jpa.hibernate.ddl-auto=none
    spring.jpa.hibernate.naming.physical-strategy=
        org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
    spring.data.jpa.repositories.enabled=true
    spring.jpa.database-platform=rg.hibernate.dialect.MySQL8Dialect
    spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect
    spring.jpa.properties.hibernate.dialect.storage_engine=innodb
    spring.jpa.properties.hibernate.format_sql=true
    

    The real fix came from fixing the Configuration class that I had. You can see what I used to have above. I shortened it to the following, and THIS is what made it work.

    @Configuration
    @PropertySource(value = "classpath:application.properties")
    @EnableAutoConfiguration
    public class RepositoryContextConfiguration  {
    }
    

    The original way I had this file, I was trying to set a whole bunch of different things. There was obviously something not being set in this Configuration class, and I didn't know what that was. Using the @EnableAutoConfiguration took care of whatever wasn't being done, and NOW it works.

    I've have been using Spring Boot since the first iterations of Spring Boot 2.x. At that time, I was still using Junit 4 for testing, and XML application-context files for configuration. As the months and years went by, I've always updated this app to the latest versions of Spring Boot and Hibernate. Whenever I updated Spring Boot, the code had to change with it, it was never a simple change.

    Anyway, I hope this issue helps someone else out. Anyway, I hope this helps someone else out.