1

I have working application with h2 database. There is no connection problem with db as I can get some values from there however when I try to create EntityManagerFactory: EntityManagerFactory emf = Persistence.createEntityManagerFactory("EmployeeService"); then I see in logs:

2020-06-04 19:22:17.901  INFO 22496 --- [           main] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [name: EmployeeService]
2020-06-04 19:22:17.946  WARN 22496 --- [           main] o.h.e.j.c.i.ConnectionProviderInitiator  : HHH000181: No appropriate connection provider encountered, assuming application will be supplying connections
2020-06-04 19:22:17.946  WARN 22496 --- [           main] o.h.e.j.e.i.JdbcEnvironmentInitiator     : HHH000342: Could not obtain connection to query metadata : The application must supply JDBC connections
Exception in thread "main" 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)
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)

in application.properties I have:

#H2
spring.h2.console.enabled=true
spring.h2.console.path=/h2-console/
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect

#Flyway
flyway.user=sa
flyway.password=
flyway.url=jdbc:h2:mem:testdb
flyway.locations=filesystem:db/migration
spring.flyway.baseline-on-migrate = true

and in persistance.xml in META-INF:

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">
    <persistence-unit name="EmployeeService">
        <class>com.example.demo.Employee.Employee</class>
        <properties>
            <property name="javax.persistence.jdbc.url" value="jdbc:h2:mem:testdb"/>
        <property name="javax.persistence.jdbc.driver" value="org.h2.Driver"/>
        <property name="javax.persistence.jdbc.user" value="sa"/>
        <property name="javax.persistence.jdbc.password" value=""/>
        </properties>
    </persistence-unit>
</persistence>

Problem appears only when try to create mentioned above EntityManagerFactory. When I don't try it I can go to h2 web console and see that flyway created db correctly, did inserts and I can do selects/inserts and so on.

@Edit Adding pom.xml

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.spingframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
        <dependency>
            <groupId>org.flywaydb</groupId>
            <artifactId>flyway-core</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>
Michu93
  • 5,058
  • 7
  • 47
  • 80
  • Does this answer your question? [Connection cannot be null when 'hibernate.dialect' not set](https://stackoverflow.com/questions/11211451/connection-cannot-be-null-when-hibernate-dialect-not-set) – fjsv Jun 04 '20 at 18:04
  • @fjsventura, unfortunately no, I saw it and it's about session factory and hibernate and as you see I have connection.url and connection.driver in my configuration files. – Michu93 Jun 04 '20 at 18:14
  • 1
    I don't think you need a persistance.xml when using Spring or Spring Boot, btw – fjsv Jun 04 '20 at 18:24
  • @fjsventura, without persistance.xml there is error: `Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named EmployeeService` – Michu93 Jun 04 '20 at 18:33
  • Is the class `Employee` annotated with `@Entity`? Also, which dependencies do you have? persistence related – fjsv Jun 04 '20 at 18:40
  • @fjsventura, yes, it's annotated as crud repo is working on `Employee` class. Added pom.xml in question. Can you please take a look? – Michu93 Jun 04 '20 at 18:57
  • 1
    Did you try to set the dialect in the persistance.xml? – chillworld Jun 04 '20 at 19:22
  • 1
    oh, im dumb, @chillworld, thank you! Can you please add as answer? – Michu93 Jun 04 '20 at 19:49
  • You shouldn't be creating the entitymanagerfactory but let Spring inject the `Entitymanager` for you. You are working around the framework creating another `EntityManagerFactory` wihch manages other db connections. That is going to hunt you in the future and lead to all sorts of weird issues. – M. Deinum Jun 09 '20 at 14:32

1 Answers1

1

If you check the exception, it's complaining that he doesn't have the dialect in your persistence.xml.

Please add

<property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>
chillworld
  • 4,207
  • 3
  • 23
  • 50