javaspringspring-boothibernatespring-mvc

Facing the error "entityManagerFactory" while creating the demo spring boot application


I have started learning the spring boot and developed the demo project but facing the below error from the last 3 weeks:

Error starting ApplicationContext. To display the condition evaluation report re-run your application with 'debug' enabled.
2024-11-02T15:34:40.878+05:30 ERROR 21676 --- [           main] o.s.boot.SpringApplication               : Application run failed

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: EntityManagerFactory interface [interface org.hibernate.SessionFactory] seems to conflict with Spring's EntityManagerFactoryInfo mixin - consider resetting the 'entityManagerFactoryInterface' property to plain [jakarta.persistence.EntityManagerFactory]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1806) ~[spring-beans-6.1.14.jar:6.1.14]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:600) ~[spring-beans-6.1.14.jar:6.1.14]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:522) ~[spring-beans-6.1.14.jar:6.1.14]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:337) ~[spring-beans-6.1.14.jar:6.1.14]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-6.1.14.jar:6.1.14]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:335) ~[spring-beans-6.1.14.jar:6.1.14]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:205) ~[spring-beans-6.1.14.jar:6.1.14]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:954) ~[spring-context-6.1.14.jar:6.1.14]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:625) ~[spring-context-6.1.14.jar:6.1.14]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:754) ~[spring-boot-3.3.5.jar:3.3.5]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:456) ~[spring-boot-3.3.5.jar:3.3.5]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:335) ~[spring-boot-3.3.5.jar:3.3.5]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1363) ~[spring-boot-3.3.5.jar:3.3.5]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1352) ~[spring-boot-3.3.5.jar:3.3.5]
    at com.example.irs.DemoGoBootSpringDataJpaApplication.main(DemoGoBootSpringDataJpaApplication.java:30) ~[classes/:na]
Caused by: java.lang.IllegalStateException: EntityManagerFactory interface [interface org.hibernate.SessionFactory] seems to conflict with Spring's EntityManagerFactoryInfo mixin - consider resetting the 'entityManagerFactoryInterface' property to plain [jakarta.persistence.EntityManagerFactory]
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.createEntityManagerFactoryProxy(AbstractEntityManagerFactoryBean.java:469) ~[spring-orm-6.1.14.jar:6.1.14]
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:403) ~[spring-orm-6.1.14.jar:6.1.14]
    at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.afterPropertiesSet(LocalContainerEntityManagerFactoryBean.java:366) ~[spring-orm-6.1.14.jar:6.1.14]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1853) ~[spring-beans-6.1.14.jar:6.1.14]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1802) ~[spring-beans-6.1.14.jar:6.1.14]
    ... 14 common frames omitted
Caused by: java.lang.IllegalArgumentException: methods with same signature getSchemaManager() but incompatible return types: [interface org.hibernate.relational.SchemaManager, interface jakarta.persistence.SchemaManager]
    at java.base/java.lang.reflect.ProxyGenerator.checkReturnTypes(ProxyGenerator.java:311) ~[na:na]
    at java.base/java.lang.reflect.ProxyGenerator.generateClassFile(ProxyGenerator.java:488) ~[na:na]
    at java.base/java.lang.reflect.ProxyGenerator.generateProxyClass(ProxyGenerator.java:178) ~[na:na]
    at java.base/java.lang.reflect.Proxy$ProxyBuilder.defineProxyClass(Proxy.java:558) ~[na:na]
    at java.base/java.lang.reflect.Proxy$ProxyBuilder.build(Proxy.java:670) ~[na:na]
    at java.base/java.lang.reflect.Proxy.lambda$getProxyConstructor$1(Proxy.java:440) ~[na:na]
    at java.base/jdk.internal.loader.AbstractClassLoaderValue$Memoizer.get(AbstractClassLoaderValue.java:329) ~[na:na]
    at java.base/jdk.internal.loader.AbstractClassLoaderValue.computeIfAbsent(AbstractClassLoaderValue.java:205) ~[na:na]
    at java.base/java.lang.reflect.Proxy.getProxyConstructor(Proxy.java:438) ~[na:na]
    at java.base/java.lang.reflect.Proxy.newProxyInstance(Proxy.java:1037) ~[na:na]
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.createEntityManagerFactoryProxy(AbstractEntityManagerFactoryBean.java:464) ~[spring-orm-6.1.14.jar:6.1.14]
    ... 18 common frames omitted

This is my project tree: enter image description here

Just ignore the DatabaseConnectionTest.java because I used it just for testing the db connection.

Below is the complete code of all the java files except the files under the exception package.

DemoGoBootSpringDataJpaApplication.java

package com.example.irs;

import java.util.Scanner;

//import org.hibernate.cfg.Environment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.PropertySource;
import org.springframework.orm.hibernate5.LocalSessionFactoryBean;

import com.example.irs.model.User;
import com.example.irs.service.RegistrationService;

@SpringBootApplication
@EntityScan(basePackages = "com.example.irs.entity")
@PropertySource(value= {"classpath:configuration.properties"})
public class DemoGoBootSpringDataJpaApplication implements CommandLineRunner{
    
//  @Autowired
//  private Environment environment;
    @Autowired
    private ApplicationContext context;

    public static void main(String[] args) {
        SpringApplication.run(DemoGoBootSpringDataJpaApplication.class, args);
    }
    
    @Override
    public void run(String ...args) throws Exception{
        try {
            User user = new User();
            Scanner sc = new Scanner(System.in);
            System.out.println("Enter user Id: ");
            String uid = sc.next();
            System.out.println("Enter password: ");
            String password = sc.next();
            System.out.println("Enter name: ");
            String name = sc.next();
            System.out.println("Enter city: ");
            String city = sc.next();
            System.out.println("Enter email: ");
            String email = sc.next();
            System.out.println("Enter phone: ");
            String phone = sc.next();
            user.setUserId(uid);
            user.setPassword(password);
            user.setName(name);
            user.setCity(city);
            user.setEmail(email);
            user.setPhone(phone);
            RegistrationService service = (RegistrationService) context.getBean("registrationService");
            String registerMessage = service.registerUser(user);
            System.out.println(registerMessage);
        }catch(Exception e) {
            System.out.println(e.getStackTrace());
        }
    }

}

UserEntity.java

package com.example.irs.entity;

import jakarta.persistence.*;

@Entity
@Table(name="USER_DETAILS")
public class UserEntity {
        @Id
        @Column(name="userId")
        private String userId;
        private String password;
        private String name;
        private String city;
        private String email;
        private String phone;
        
        public UserEntity() {
            
        }
        public String getUserId() {
            return userId;
        }
        public void setUserId(String userId) {
            this.userId = userId;
        }
        public String getPassword() {
            return password;
        }
        public void setPassword(String password) {
            this.password = password;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public String getCity() {
            return city;
        }
        public void setCity(String city) {
            this.city = city;
        }
        public String getEmail() {
            return email;
        }
        public void setEmail(String email) {
            this.email = email;
        }
        public String getPhone() {
            return phone;
        }
        public void setPhone(String phone) {
            this.phone = phone;
        }
        
        
}

User.java

package com.example.irs.model;

public class User {
    private String userId;
    private String password;
    private String name;
    private String city;
    private String email;
    private String phone;
    public String getUserId() {
        return userId;
    }
    public void setUserId(String userId) {
        this.userId = userId;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getCity() {
        return city;
    }
    public void setCity(String city) {
        this.city = city;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public String getPhone() {
        return phone;
    }
    public void setPhone(String phone) {
        this.phone = phone;
    }
    
    

}

UserRepository.java

package com.example.irs.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import com.example.irs.entity.UserEntity;

@Repository
public interface UserRepository extends JpaRepository<UserEntity, String>{

}

RegistrationService.java

package com.example.irs.service;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.naming.InvalidNameException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.example.irs.entity.UserEntity;
import com.example.irs.exception.DemoGoBootException;
import com.example.irs.exception.InvalidCityException;
import com.example.irs.exception.InvalidEmailException;
import com.example.irs.exception.InvalidPasswordException;
import com.example.irs.exception.InvalidPhoneException;
import com.example.irs.exception.InvalidUserIdException;
import com.example.irs.exception.UserIdAlreadyPresentException;
import com.example.irs.model.User;
import com.example.irs.repository.UserRepository;

@Service
public class RegistrationService {
    
    @Autowired
    private UserRepository userRepository;
    String regex1 = "^[a-zA-z0-9]{4,15}+$";
    public String registerUser(User user) throws DemoGoBootException, UserIdAlreadyPresentException{
        validateUser(user);
        boolean b = userRepository.existsById(user.getUserId());
        if(b) {
            throw new UserIdAlreadyPresentException("RegistrationService.USERID_PRESENT");
        }
        UserEntity userEntity = new UserEntity();
        userEntity.setCity(user.getCity());
        userEntity.setEmail(user.getEmail());
        userEntity.setName(user.getName());
        userEntity.setPassword(user.getPassword());
        userEntity.setPhone(user.getPhone());
        userEntity.setUserId(user.getUserId());
        userRepository.saveAndFlush(userEntity);
        return "UserRepository.REGISTRATION_SUCCESS";
        
    }
    
    public void validateUser(User user) throws DemoGoBootException{
        if(!isValidUserId(user.getUserId())) {
            try {
                throw new InvalidUserIdException("RegistrationService.INVALID_USERID");
            }catch(InvalidUserIdException e) {
                e.printStackTrace();
            }
        }
        if(!isValidPassword(user.getPassword())) {
            try {
                throw new InvalidPasswordException("RegistrationService.INVALID_PASSWORD");
            }catch(InvalidPasswordException e) {
                e.printStackTrace();
            }
        }
        if(!isValidName(user.getName())) {
            try {
                throw new InvalidNameException("RegistrationService.INVALID_NAME");
            }catch(InvalidNameException e) {
                e.printStackTrace();
            }
        }
        if(!isValidCity(user.getCity())) {
            try {
                throw new InvalidCityException("RegistrationService.INVALID_CITY");
            }catch(InvalidCityException e) {
                e.printStackTrace();
            }
        }
        if(!isValidEmail(user.getEmail())) {
            try {
                throw new InvalidEmailException("RegistrationService.INVALID_EMAIL");
            }catch(InvalidEmailException e) {
                e.printStackTrace();
            }
        }
        if(!isValidPhoneNumber(user.getPhone())) {
            try {
                throw new InvalidPhoneException("RegistrationService.INVALID_PHONE");
            }catch(InvalidPhoneException e) {
                e.printStackTrace();
            }
        }
    }
    
    public boolean isValidUserId(String userId) {
        Pattern pattern1 = Pattern.compile(regex1);
        Matcher matcher1 = pattern1.matcher(userId);
        if(matcher1.matches()) return true;
        return false;
    }
    
    public boolean isValidPassword(String password) {
        String regex2 = "^[a-zA-Z0-9]{8,15}+$";
        Pattern pattern1 = Pattern.compile(regex2);
        Matcher matcher1 = pattern1.matcher(password);
        if(matcher1.matches()) return true;
        return false;
    }
    
    public boolean isValidName(String name) {
        Pattern pattern1 = Pattern.compile(regex1);
        Matcher matcher1 = pattern1.matcher(name);
        if(matcher1.matches()) return true;
        return false;
    }
    
    public boolean isValidCity(String city) {
        Pattern pattern1 = Pattern.compile(regex1);
        Matcher matcher1 = pattern1.matcher(city);
        if(matcher1.matches()) return true;
        return false;
    }
    
    public boolean isValidEmail(String email) {
        String regex2 = "^[A-Za-z0-9+_.-]+@(.+)$";
        Pattern pattern1 = Pattern.compile(regex2);
        Matcher matcher1 = pattern1.matcher(email);
        if(matcher1.matches()) return true;
        return false;
    }
    
    public boolean isValidPhoneNumber(String number) {
        String regex2 = "[0-9]{10}";
        Pattern pattern1 = Pattern.compile(regex2);
        Matcher matcher1 = pattern1.matcher(number);
        if(matcher1.matches()) return true;
        return false;
    }

}

The below is the code of the DemoGoBootException.java and the same code is in all the exception files.

DemoGoBootException.java

package com.example.irs.exception;

public class DemoGoBootException extends Exception{
    private static final long serialVersionUID = 1L;
    public DemoGoBootException(String message) {
        super(message);
    }
}

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.3.5</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example.irs</groupId>
    <artifactId>DemoGoBoot_SpringDataJPA</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>DemoGoBoot_SpringDataJPA</name>
    <description>Implementing repository layer with Spring Data JPA</description>
    <url/>
    <licenses>
        <license/>
    </licenses>
    <developers>
        <developer/>
    </developers>
    <scm>
        <connection/>
        <developerConnection/>
        <tag/>
        <url/>
    </scm>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <!-- <dependency>
            <groupId>com.mysql</groupId>
            <artifactId>mysql-connector-j</artifactId>
            <scope>runtime</scope>
        </dependency>-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.33</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>jakarta.persistence</groupId>
            <artifactId>jakarta.persistence-api</artifactId>
            <version>3.2.0</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>6.6.1.Final</version>
            <type>pom</type>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>5.2.3.Final</version>
        </dependency>
        <dependency>
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>
            <version>2.3.0</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

application.properties

spring.datasource.url = jdbc:mysql://127.0.0.1:3306/db_irs_dev
spring.datasource.username = root
spring.datasource.password = Batman@123Strong
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect

Please help me with this error, I'm fed up with this.

Note: I used ChatGPT as well but it didn't worked and used the solution of the similar StackOverflow questions, but nothing worked.


Solution

  • The error you are encountering is because of an incompatibility between Hibernate Core Version and Jakarta Persistence API dependencies you're using in your POM file.

    Basically, you don't need to include all this dependencies related to Persistence Layer explicitly, all you need in your project is:

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>8.0.33</version>
    </dependency> 
    

    spring-boot-starter-data-jpa includes both Hibernate and Jakarta Persistence API and this provides you all you need for interacting with you db. So your pom.xml file should look like this:

       <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.33</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>
            <version>2.3.0</version>
        </dependency>
    </dependencies>
    

    Run maven command to clean-install and hopefully the error will vanish.