Reputation: 465
I have this error when running a springboot app:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: UUID
I tried to troubleshot this issue but I only find pages describing a similar issue but with "Invocation of init method failed" instead "UUID".
"UUID" is not where to be found when it comes to the "Error creating bean with name 'entityManagerFactory' defined in class path resource" error message :/
chatGPT is telling me: "The org.springframework.beans.factory.BeanCreationException you're encountering with the message "Error creating bean with name 'entityManagerFactory'" typically indicates an issue with the configuration of the entityManagerFactory bean, which is responsible for managing the JPA (Java Persistence API) entity manager in a Spring Boot application.
The error message includes a reference to the HibernateJpaConfiguration.class and the term "UUID." This suggests that there might be a mismatch in the versions of the libraries you are using, especially related to Hibernate."
This makes a lot of sense however I have double checked all compatibility on maven repository and it looks I am up to date. Here is my pom.xml:
<?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.1.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.globomatics</groupId>
<artifactId>bike</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>bike</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>17</java.version>
<spring.boot.version>3.1.3</spring.boot.version>
</properties>
<dependencies>
<!-- Other dependencies -->
<!-- Spring Boot Starter Web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${spring.boot.version}</version>
</dependency>
<!-- Spring Boot Starter Data JPA -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>${spring.boot.version}</version>
</dependency>
<!-- SQLite Driver -->
<dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
<version>3.42.0.0</version>
</dependency>
<!-- Hibernate-->
<dependency>
<groupId>org.hibernate.orm</groupId>
<artifactId>hibernate-core</artifactId>
<version>6.2.7.Final</version>
</dependency>
<dependency>
<groupId>jakarta.persistence</groupId>
<artifactId>jakarta.persistence-api</artifactId>
<version>3.1.0</version>
</dependency>
<!-- Spring Boot Starter Test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>${spring.boot.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>${spring.boot.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<!-- Spring Boot Maven Plugin -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Moreover, I fail to understand this "Error creating bean with name 'entityManagerFactory'" since I am using spring data jpa repository and I don't have to configure entityManagerFactory nor datasource in this case.
Here is also my application.properties
spring.jpa.database-platform=org.hibernate.dialect.SQLiteDialect
spring.jpa.hibernate.ddl-auto=none
spring.jpa.show-sql=true
spring.datasource.url=jdbc:sqlite:bike.db
spring.datasource.username=
spring.datasource.password=
spring.datasource.driver-class-name=org.sqlite.JDBC
Also some information on my annotations that should be correct:
@SpringBootApplication
@EntityScan("com.globomatics.bike.models")
@EnableJpaRepositories("com.globomatics.bike.repositories")
public class BikeApplication {
public static void main(String[] args) {
SpringApplication.run(BikeApplication.class, args);
}
}
@RestController
@RequestMapping("/api/v1/bikes")
public class BikeController {
private final BikeRepository bikeRepository;
@Autowired
public BikeController(BikeRepository bikeRepository) {
this.bikeRepository = bikeRepository;
}
@GetMapping
public List<Bike> list() {
return bikeRepository.findAll();
}
@PostMapping
@ResponseStatus(HttpStatus.OK)
public void create(@RequestBody Bike bike){
bikeRepository.save(bike);
}
@GetMapping("/{id}")
public Bike get(@PathVariable("id") long id){
return bikeRepository.getReferenceById(id);
}
}
@Entity
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
public class Bike {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
private String email;
private String phone;
private String model;
private String serialNumber;
private BigDecimal purchasePrice;
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "MM-dd-yyyy")
private Date purchaseDate;
private boolean contact;
// GETTERS AND SETTERS
}
finally, standard JPA repo interface:
public interface BikeRepository extends JpaRepository <Bike, Long> {
}
This is from the following class: https://app.pluralsight.com/library/courses/building-first-app-with-spring-boot-angularjs/table-of-contents
The content was poorly updated in the past few years. And I am trying to implement the main changes since springboot 3.0++ considering some major dependency migrations.
Some dependency compatibilies might be the cause of the issue.
Also the error "Error creating bean with name 'entityManagerFactory'" is terribly misleading to me as mentioned above.
So after 2 days between searches, docs, mvn clean install, trials and errors, random guessing between versionning and chatgpt, I drop a message here asking for help.
Thanks a lot.
Charles
Upvotes: 3
Views: 22207
Reputation: 426
I got the exact same error for postgres and h2 for my test profile. I fixed it by inspecting my dependency tree
./gradlew dependencies --configuration runtimeClasspath
And,found out that I have jakarta.persistence:jakarta.persistence-api:3.0.0 in my dependency list which is in conflict with the version of the same library comes with org.springframework.boot:spring-boot-starter-data-jpa 3.3.1.
If you have this issue, please also check and make sure your dependencies and make sure that you don't have multiple versions of the same dependency.
Upvotes: 0
Reputation: 465
Ok I have finally solved this.
1) Hibernate community is key
First it is important to understand that since Hibernate 6+, sqlite dialect has been moved from "hibernate core" to "hibernate community dialect". This was supported in some of my research:
https://www.baeldung.com/spring-boot-sqlite
https://discourse.hibernate.org/t/varargssqlfunction-was-deleted-hibernate-6-0s-development/6826
So I had to add explicitly
spring.jpa.properties.hibernate.dialect=org.hibernate.community.dialect.SQLiteDialect
At that point, unlike previously, my project will build successfuly. However I will still fail to start.
2) Hibernate core is in sping data jpa and must be excluded
Second, Spring Data JPA includes Hibernate as its default JPA provider. In my case "spring-data-jpa" includes "Hibernate core 6.2.7.FINAL". This one is used by default when you run your app and springboot is looking for sqlite dialect in Hibernate core, which does not exist.
So the trick here is to to add an exclusion of hibernate core to your spring data jpa dependency in your pom.xml
To summarize, you'll find my pom and my application.properties.
# Hibernate Configuration
spring.jpa.hibernate.ddl-auto=none
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.community.dialect.SQLiteDialect
# Datasource config for Sqlite
spring.datasource.url=jdbc:sqlite:bike.db
spring.datasource.username=
spring.datasource.password=
spring.datasource.driver-class-name=org.sqlite.JDBC
<?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.1.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.globomatics</groupId>
<artifactId>bike</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>bike</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>17</java.version>
<spring.boot.version>3.1.3</spring.boot.version>
</properties>
<dependencies>
<!-- Other dependencies -->
<!-- Spring Boot Starter Web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${spring.boot.version}</version>
</dependency>
<!-- SQLite Driver -->
<dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
<version>3.44.1.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hibernate.orm/hibernate-community-dialects-->
<dependency>
<groupId>org.hibernate.orm</groupId>
<artifactId>hibernate-community-dialects</artifactId>
<version>6.2.7.Final</version>
</dependency>
<!-- Spring Boot Starter Data JPA -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>${spring.boot.version}</version>
<exclusions>
<exclusion>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- Spring Boot Starter Test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>${spring.boot.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>${spring.boot.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<!-- Spring Boot Maven Plugin -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Upvotes: 1