当前位置: 首页 > 知识库问答 >
问题:

没有为类型类User找到属性findOne()

郑正阳
2023-03-14

我已经搜索了很多页,但没有找到答案,所以我粘贴了整个代码。我正在测试测试类,并得到这样的错误:“起因:org.springframework.beans.factory.BeanCreation异常:创建名为“userRepository”的bean时出错:初始化方法调用失败;嵌套异常java.lang.IllegalArgument异常:未能为方法公共抽象创建查询org.home.mysystem.entity.Userorg.home.mysystem.repository.UserRepository.findOne(java.lang.String)!没有为类型User找到属性findOne!”。请帮助我

角色JAVA

package org.home.mysystem.entity;

import java.util.List;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.ManyToMany;

@Entity
public class Role {

    @Id
    private String name;
    @ManyToMany(mappedBy = "roles")
    private List<User> users;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<User> getUsers() {
        return users;
    }

    public void setUsers(List<User> users) {
        this.users = users;
    }

    public Role(String name, List<User> users) {
        this.name = name;
        this.users = users;
    }

    public Role() {
    }

    public Role(String name) {
        this.name = name;
    }

}

任务JAVA

package org.home.mysystem.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;

import org.hibernate.validator.constraints.NotEmpty;

@Entity
public class Task {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    @NotEmpty
    private String date;
    @NotEmpty
    private String startTime;
    @NotEmpty
    private String stopTime;
    @NotEmpty
    @Column(length=1000)
    private String description;
    @ManyToOne
    @JoinColumn(name="USER_EMAIL")
    private User user;

    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public String getDate() {
        return date;
    }
    public void setDate(String date) {
        this.date = date;
    }
    public String getStartTime() {
        return startTime;
    }
    public void setStartTime(String startTime) {
        this.startTime = startTime;
    }
    public String getStopTime() {
        return stopTime;
    }
    public void setStopTime(String stopTime) {
        this.stopTime = stopTime;
    }
    public String getDescription() {
        return description;
    }
    public void setDescription(String description) {
        this.description = description;
    }
    public User getUser() {
        return user;
    }
    public void setUser(User user) {
        this.user = user;
    }
    public Task(String date, String startTime, String stopTime, String description, User user) {
        this.date = date;
        this.startTime = startTime;
        this.stopTime = stopTime;
        this.description = description;
        this.user = user;
    }
    public Task(String date, String startTime, String stopTime, String description) {
        this.date = date;
        this.startTime = startTime;
        this.stopTime = stopTime;
        this.description = description;
    }
    public Task() {
    }



}

User.java

package org.home.mysystem.entity;

import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.validation.constraints.Size;

import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.NotEmpty;

@Entity
public class User {

    @Id
    @Email
    @NotEmpty
    @Column(unique = true)
    private String email;
    @NotEmpty
    private String name;
    @Size(min = 4)
    private String password;
    @OneToMany(mappedBy = "user", cascade = CascadeType.ALL)
    private List<Task> tasks;

    @ManyToMany(cascade = CascadeType.ALL)
    @JoinTable(name = "USER_ROLES", joinColumns={
            @JoinColumn(name = "USER_EMAIL", referencedColumnName = "email") }, inverseJoinColumns = {
                    @JoinColumn(name = "ROLE_NAME", referencedColumnName = "name") })
    private List<Role> roles;

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public List<Task> gettasks() {
        return tasks;
    }

    public void settasks(List<Task> tasks) {
        this.tasks = tasks;
    }

    public List<Role> getRoles() {
        return roles;
    }

    public void setRoles(List<Role> roles) {
        this.roles = roles;
    }

    public User(String email, String name, String password) {
        this.email = email;
        this.name = name;
        this.password = password;
    }

    public User() {

    }

}

角色还原。JAVA

    package org.home.mysystem.repository;

    import org.home.mysystem.entity.Role;
    import org.springframework.data.jpa.repository.JpaRepository;

    public interface RoleRepository extends JpaRepository<Role, String> {

    }

TaskRepository。JAVA

 public interface TaskRepository extends JpaRepository<Task, Long> {

        List<Task> findByUser(User user);

    }

用户存储库。JAVA

package org.home.mysystem.repository;

import org.home.mysystem.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;

public interface UserRepository extends JpaRepository<User,String> {

    User findOne(final String email);

}

任务ervice.java

 package org.home.mysystem.service;

    import java.util.ArrayList;
    import java.util.List;

    import org.home.mysystem.entity.Role;
    import org.home.mysystem.entity.Task;
    import org.home.mysystem.entity.User;
    import org.home.mysystem.repository.TaskRepository;
    import org.home.mysystem.repository.UserRepository;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
    import org.springframework.stereotype.Service;



    @Service
    public class TaskService {

        @Autowired
        private TaskRepository taskRepository;

        public void addTask(Task task, User user) {
            task.setUser(user);
            taskRepository.save(task);
        }

        public List<Task>  findUserTask(User user){

            return taskRepository.findByUser(user);
        }
    }

用户ervice.java

import java.util.ArrayList;
import java.util.List;

import org.home.mysystem.entity.Role;
import org.home.mysystem.entity.User;
import org.home.mysystem.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;



@Service
public class UserService {

    @Autowired
    private UserRepository userRepository;

    public void createUser(User user) {
        BCryptPasswordEncoder encoder=new BCryptPasswordEncoder();
        user.setPassword(encoder.encode(user.getPassword()));
        Role userRole=new Role("USER");
        List<Role> roles=new ArrayList<>();
        roles.add(userRole);
        user.setRoles(roles);
        userRepository.save(user);
    }

    public void createAdmin(User user) {
        BCryptPasswordEncoder encoder=new BCryptPasswordEncoder();
        user.setPassword(encoder.encode(user.getPassword()));
        Role userRole=new Role("ADMIN");
        List<Role> roles=new ArrayList<>();
        roles.add(userRole);
        user.setRoles(roles);
        userRepository.save(user);
    }

    public User findOne(String email) {
        return userRepository.findOne(email);
    }
}

MyApplicationest.java

package org.home.mysystem;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import java.util.List;

import org.home.mysystem.entity.Task;
import org.home.mysystem.entity.User;
import org.home.mysystem.service.TaskService;
import org.home.mysystem.service.UserService;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class MySystemApplicationTests {

    @Autowired
    private UserService userService;

    @Autowired
    private TaskService taskService;

    @Before
    public void initDb() {
      {
          User newUser = new User("testUser@mail.com", "testUser", "123456");
          userService.createUser(newUser); 
      }
      {
          User newUser = new User("testAdmin@mail.com", "testAdmin", "123456");
          userService.createUser(newUser); 
      }

      Task userTask = new Task("03/01/2018", "00:11", "11:00", "You need to work today");
      User user = userService.findOne("testUser@mail.com");
      taskService.addTask(userTask, user);  

    }

    @Test
    public void testUser() {
        User user=userService.findOne("testUser@mail.com");
        assertNotNull(user);
        User admin=userService.findOne("testAdmin@mail.com");
        assertEquals(admin.getEmail(),"testAdmin@mail.com");
    }

    @Test
    public void testTask() {
        User user=userService.findOne("testUser@mail.com");
        List<Task> task=taskService.findUserTask(user);
        assertNotNull(task);
    }

    }

共有1个答案

舒俊雄
2023-03-14

问题是Spring在你给予它的时候期待着别的东西。

默认情况下,findOne被定义为使用ID(主键)加载实体。所以它期望一个longlong(据我所知)。它使用给定参数(电子邮件)的名称,并正在搜索具有该名称的ID,但这根本不符合要求。

如果您想通过电子邮件或您定义的其他字段进行搜索,您需要使用以下语法:

例1

要搜索的示例字段:电子邮件

存储库中的方法:User findByEmail(String email)

例2

要搜索的示例字段:username

存储库中的方法:User findByUsername(字符串用户名)

我希望这能有所帮助!

 类似资料:
  • 您好,我正在建立一个动物园微服务,包括动物、员工、客户和价格表。我的动物微服务可以工作,但在我的员工微服务中,我遇到了一个错误,即没有为类型“EmployeeModel”找到属性“name”。在问这个问题之前,我已经在网上搜索了几个小时,还有一些类似的问题。我在模型中没有“名字”employee_name,我只是感到困惑,不知道如何修复它。任何指导/建议/信息将不胜感激:)第一次发布,所以我希望我

  • 我正在尝试将ReactiveQuerydslPredicateExecitor与R2dbcRepository一起用于我的spring-data-r2dbc应用程序以及r2dbc-mssql客户端版本0.8.1。释放 我从2.2.7升级了spring boot版本。发布至2.3.0。版本和1.0.0的spring-data-r2dbc版本。发布至1.1.0。释放。我正在使用query dsl版本4

  • 我有以下实体: 然后我创建我的存储库: 但我一开始就收到: 未找到MyTable类型的属性getCountOf 我到底做错了什么?

  • 我最近开始使用ReactiveCouchbase Repository(sping-data-Couchbase-3.0.0。M2,Spring-引导-启动器-父-2.0.0。M2)在我们的一个项目中。 我从这里引用了未发布的文档并设置了项目,但我得到了以下错误。 以下是关于设置我的项目的相关信息。 @Maven @爪哇 现在,我确实经历了这个和这个,但在这里,我没有在DummyRepositor

  • 我不知道为什么Spring不喜欢我的代码: 我有: : : < code > entryserviceimpl . Java : 此代码给我一个错误: org.springframework.beans.factory。BeanCreationException:创建名为“entryDao”的bean时出错:调用init方法失败;嵌套异常是org.springframework.data.map。