【问题标题】:Error creating bean with name 'userRepo' defined in com.example.jpa.repository创建 com.example.jpa.repository 中定义的名称为“userRepo”的 bean 时出错
【发布时间】:2021-07-30 20:09:13
【问题描述】:

这是我在编译时遇到的 tge 错误。我是春天的新手,无法理解错误。请帮助我找出答案。

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userRepo' defined in com.example.jpa.repository.UserRepo defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Invocation of init method failed; nested exception is org.springframework.data.repository.query.QueryCreationException: Could not create query for public abstract java.util.List com.example.jpa.repository.UserRepo.findNameLike(java.lang.String)! Reason: Failed to create query for method public abstract java.util.List com.example.jpa.repository.UserRepo.findNameLike(java.lang.String)! No property findName found for type User!; nested exception is java.lang.IllegalArgumentException: Failed to create query for method public abstract java.util.List com.example.jpa.repository.UserRepo.findNameLike(java.lang.String)! No property findName found for type User!

这是我的存储库类

根据 @Query 中的错误,但我正在了解如何修复以及我的错误在哪里。 我在 mysql 中创建了一个名为 user1 的 tavle。

package com.example.jpa.repository;

import java.util.List;

import javax.persistence.criteria.CriteriaBuilder.In;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

import com.example.jpa.entity.User;

@Repository
public interface UserRepo extends JpaRepository<User, Integer> {
    public List<User> findByAgeGreaterThanEqual(int age) ;
    
    public List<User> findByAgeIn(int age);
    
    public List<User> findByNameOrderByName(String name);
    @Modifying
    @Query (value="select * from user1",nativeQuery=true)
    public List<User> getAllUser();
}

这是我的实体类

package com.example.jpa.entity;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
//@Table(name = "user")
public class User {
    
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;
    private String name;
    private String city;
    private String status;
    
    public User() {
        super();
        
    }
    public User(int id, String name, String city, String status) {
        super();
        this.id = id;
        this.name = name;
        this.city = city;
        this.status = status;
    }
    
    
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    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 getStatus() {
        return status;
    }
    public void setStatus(String status) {
        this.status = status;
    }
}

这是我的主要应用程序主类

package com.example.jpa;

import java.util.List;
import java.util.Optional;

import org.apache.catalina.core.ApplicationContext;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

import com.example.jpa.entity.User;
import com.example.jpa.repository.UserRepo;


@SpringBootApplication
public class SpringApplicaton1Application {

    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(SpringApplicaton1Application.class, args);
        UserRepo repo = context.getBean(UserRepo.class);

        User user = new User();
        user.setId(1);
        user.setCity("Delhi");
        user.setName("Ramesh");
        user.setStatus("Java Developer");

        List<User> users= repo.getAllUser();
        
        users.forEach(n->{
            System.out.println(n);
            
        });
        
    }

}

这是我的 Application.property 文件

spring.datasource.name=test
spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:3306/practice   
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL55Dialect

spring.jpa.hibernate.use-new-id-generator-mappings= false

spring.jpa.hibernate.ddl-auto=update

【问题讨论】:

  • 您的错误信息与您的代码不匹配;您没有在此代码中定义查询方法findNameLike。此外,(1) nativeQuery绝对不得已的手段;您的查询可以轻松地用 JPQL 编写,并且 (2) findAll 已经隐式声明。
  • 非常感谢我能够解决 pblm。

标签: java spring jpa crud


【解决方案1】:

使用 2 个注解,例如 enablejparepositories 、 entityscan

【讨论】:

  • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
猜你喜欢
  • 2020-09-15
  • 1970-01-01
  • 2015-09-24
  • 2017-08-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-07-28
相关资源
最近更新 更多