maven Sping Boot 和Java JPA

nlejzf6q  于 2023-06-21  发布在  Maven
关注(0)|答案(2)|浏览(140)

我遇到了以下错误:

Error starting ApplicationContext. To display the condition evaluation report re-run your application with 'debug' enabled.
2023-06-12T21:33:06.984+03:00 ERROR 10508 --- [           main] o.s.boot.SpringApplication               : Application run failed

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'supermarketApplication': Unsatisfied dependency expressed through field 'userRepository': Error creating bean with name 'userRepository': Failed to convert property value of type 'jdk.proxy2.$Proxy96 implementing org.hibernate.Session,org.springframework.orm.jpa.EntityManagerProxy' to required type 'javax.persistence.EntityManager' for property 'entityManager'; Cannot convert value of type 'jdk.proxy2.$Proxy96 implementing org.hibernate.Session,org.springframework.orm.jpa.EntityManagerProxy' to required type 'javax.persistence.EntityManager' for property 'entityManager': no matching editors or conversion strategy found
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:713) ~[spring-beans-6.0.8.jar:6.0.8]
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:693) ~[spring-beans-6.0.8.jar:6.0.8]
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:133) ~[spring-beans-6.0.8.jar:6.0.8]
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:482) ~[spring-beans-6.0.8.jar:6.0.8]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1416) ~[spring-beans-6.0.8.jar:6.0.8]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:597) ~[spring-beans-6.0.8.jar:6.0.8]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:520) ~[spring-beans-6.0.8.jar:6.0.8]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:326) ~[spring-beans-6.0.8.jar:6.0.8]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-6.0.8.jar:6.0.8]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:324) ~[spring-beans-6.0.8.jar:6.0.8]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200) ~[spring-beans-6.0.8.jar:6.0.8]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:973) ~[spring-beans-6.0.8.jar:6.0.8]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:917) ~[spring-context-6.0.8.jar:6.0.8]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:584) ~[spring-context-6.0.8.jar:6.0.8]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:146) ~[spring-boot-3.0.6.jar:3.0.6]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:732) ~[spring-boot-3.0.6.jar:3.0.6]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:434) ~[spring-boot-3.0.6.jar:3.0.6]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:310) ~[spring-boot-3.0.6.jar:3.0.6]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1304) ~[spring-boot-3.0.6.jar:3.0.6]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1293) ~[spring-boot-3.0.6.jar:3.0.6]
    at com.softeng.supermarket.SupermarketApplication.main(SupermarketApplication.java:21) ~[classes/:na]
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userRepository': Failed to convert property value of type 'jdk.proxy2.$Proxy96 implementing org.hibernate.Session,org.springframework.orm.jpa.EntityManagerProxy' to required type 'javax.persistence.EntityManager' for property 'entityManager'; Cannot convert value of type 'jdk.proxy2.$Proxy96 implementing org.hibernate.Session,org.springframework.orm.jpa.EntityManagerProxy' to required type 'javax.persistence.EntityManager' for property 'entityManager': no matching editors or conversion strategy found
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:605) ~[spring-beans-6.0.8.jar:6.0.8]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:520) ~[spring-beans-6.0.8.jar:6.0.8]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:326) ~[spring-beans-6.0.8.jar:6.0.8]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-6.0.8.jar:6.0.8]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:324) ~[spring-beans-6.0.8.jar:6.0.8]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200) ~[spring-beans-6.0.8.jar:6.0.8]
    at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:254) ~[spring-beans-6.0.8.jar:6.0.8]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1417) ~[spring-beans-6.0.8.jar:6.0.8]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1337) ~[spring-beans-6.0.8.jar:6.0.8]
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:710) ~[spring-beans-6.0.8.jar:6.0.8]
    ... 20 common frames omitted
Caused by: org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'jdk.proxy2.$Proxy96 implementing org.hibernate.Session,org.springframework.orm.jpa.EntityManagerProxy' to required type 'javax.persistence.EntityManager' for property 'entityManager'; Cannot convert value of type 'jdk.proxy2.$Proxy96 implementing org.hibernate.Session,org.springframework.orm.jpa.EntityManagerProxy' to required type 'javax.persistence.EntityManager' for property 'entityManager': no matching editors or conversion strategy found
    at org.springframework.beans.AbstractNestablePropertyAccessor.convertIfNecessary(AbstractNestablePropertyAccessor.java:594) ~[spring-beans-6.0.8.jar:6.0.8]
    at org.springframework.beans.AbstractNestablePropertyAccessor.convertForProperty(AbstractNestablePropertyAccessor.java:608) ~[spring-beans-6.0.8.jar:6.0.8]
    at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:190) ~[spring-beans-6.0.8.jar:6.0.8]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.convertForProperty(AbstractAutowireCapableBeanFactory.java:1730) ~[spring-beans-6.0.8.jar:6.0.8]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1687) ~[spring-beans-6.0.8.jar:6.0.8]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1431) ~[spring-beans-6.0.8.jar:6.0.8]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:597) ~[spring-beans-6.0.8.jar:6.0.8]
    ... 29 common frames omitted
Caused by: java.lang.IllegalStateException: Cannot convert value of type 'jdk.proxy2.$Proxy96 implementing org.hibernate.Session,org.springframework.orm.jpa.EntityManagerProxy' to required type 'javax.persistence.EntityManager' for property 'entityManager': no matching editors or conversion strategy found
    at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:259) ~[spring-beans-6.0.8.jar:6.0.8]
    at org.springframework.beans.AbstractNestablePropertyAccessor.convertIfNecessary(AbstractNestablePropertyAccessor.java:589) ~[spring-beans-6.0.8.jar:6.0.8]
    ... 35 common frames omitted

我的文件如下:
pom.xml依赖项

<dependencies>
        <!-- https://mvnrepository.com/artifact/nz.net.ultraq.thymeleaf/thymeleaf-layout-dialect -->
        <dependency>
            <groupId>nz.net.ultraq.thymeleaf</groupId>
            <artifactId>thymeleaf-layout-dialect</artifactId>
            <version>3.2.1</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>com.mysql</groupId>
            <artifactId>mysql-connector-j</artifactId>
            <scope>runtime</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-jdbc -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-validation -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</artifactId>
            <version>3.1.0</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/jakarta.validation/jakarta.validation-api -->
        <dependency>
            <groupId>jakarta.validation</groupId>
            <artifactId>jakarta.validation-api</artifactId>
            <version>3.0.2</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-commons</artifactId>
            <version>3.1.0</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-jpa -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
            <version>3.1.0</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>6.1.7.Final</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-entitymanager -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>6.0.0.Alpha7</version>
            <type>pom</type>
        </dependency>
    </dependencies>

用户类

package com.softeng.supermarket.models;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.validation.constraints.Email;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;

@Entity
public class User {

    @Id
    @NotBlank(message = "Username is required")
    @Size(min=5,max=15,message = "Username must be between 5 and 15 characters")
    private String Username;
    @NotBlank(message = "Password is required")
    @Size(min=8,max=15,message = "Password must be between 8 and 15 characters")
    private String Password;

    private String Email;
    private String Phone;

    private String Firstname;

    private String Lastname;
    private Boolean isAdmin;

    public User() {
    }

    public User(String username, String password, String email) {
        this.Username = username;
        this.Password = password;
        this.Email = email;
    }

    public User(String username, String password, String email, String phone, String firstname, String lastname, Boolean isAdmin) {
        Username = username;
        Password = password;
        Email = email;
        Phone = phone;
        Firstname = firstname;
        Lastname = lastname;
        this.isAdmin = isAdmin;
    }
     + getters/setters
}

UserRepository

package com.softeng.supermarket.repositories;

import com.softeng.supermarket.models.User;
import org.springframework.data.repository.CrudRepository;

import java.util.List;

public interface UserRepository extends CrudRepository<User, String> {
    List<User> findByUsername(String name);
    List<User> findByPhoneNumber(String phoneNumber);
}

最后是main

package com.softeng.supermarket;

import com.softeng.supermarket.models.User;
import com.softeng.supermarket.repositories.UserRepository;
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.context.annotation.ComponentScan;

@SpringBootApplication
@ComponentScan("com.softeng.supermarket.controllers")
@ComponentScan("com.softeng.supermarket.models")
@ComponentScan("com.softeng")
public class SupermarketApplication implements CommandLineRunner {

    @Autowired
    UserRepository userRepository;

    public static void main(String[] args) {
        SpringApplication.run(SupermarketApplication.class, args);
    }

    @Override
    public void run(String... args){
        User user1 = new User("Antonis","test123","test@tester.com","6971989277","Antonios","Fritzelas",false);
        userRepository.save(user1);
        userRepository.findByUsername("Antonis").forEach(
                val -> System.out.println(val)
        );
    }
}

我试过各种版本的starter jpa,spring Boot 和hibernate,一直有问题。会有什么问题呢?我已经搜索了各种论坛,但无法找到任何解决方案
先谢谢你了!

u1ehiz5o

u1ehiz5o1#

我认为你正在使用最新版本的Sping Boot ,但是其他依赖版本与Spring Boot版本不兼容。尝试从XML中删除version元素,Sping Boot 的依赖项管理将自动选择兼容的版本。

bihw5rsg

bihw5rsg2#

我认为你不应该使用用户名作为ID。请尝试自动生成ID。另外,如果你想让用户名唯一,你可以使用column注解来设置它:

@Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  private Long id;

  @Column(unique = true)
  private String username;

  // Dont forget to update your repository for Long id:
   public interface UserRepository extends CrudRepository<User, Long> {

相关问题