创建演示 Spring Boot 应用程序时遇到错误“entityManagerFactory”

问题描述 投票:0回答:1

我已经开始学习 Spring Boot 并开发了演示项目,但在过去 3 周中遇到了以下错误:

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

这是我的项目树: enter image description here

忽略DatabaseConnectionTest.java,因为我只是用它来测试数据库连接。

下面是除了异常包下的文件之外的所有java文件的完整代码

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());
        }
    }

}

用户实体.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;
        }
        
        
}

用户.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;
    }
    
    

}

用户存储库.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>{

}

注册服务.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;
    }

}

以下是 DemoGoBootException.java 的代码,所有异常文件中都有相同的代码。

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>

应用程序.属性

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

请帮我解决这个错误,我受够了这个。

注意:我也使用了ChatGPT,但它不起作用,并使用了类似StackOverflow问题的解决方案,但没有任何作用。

java spring spring-boot hibernate spring-mvc
1个回答
0
投票

您遇到的错误是因为您在 POM 文件中使用的 Hibernate Core VersionJakarta Persistence API 依赖项之间不兼容。

基本上,您不需要显式地包含与 Persistence Layer 相关的所有依赖项,您的项目中只需要:

<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 包括 Hibernate 和 Jakarta Persistence API,这为您提供了与数据库交互所需的一切。

© www.soinside.com 2019 - 2024. All rights reserved.