找不到类型为User的属性findOne()

时间:2018-04-13 17:09:33

标签: spring hibernate spring-mvc spring-boot spring-data-jpa

我搜索了很多页面,但没有找到答案,所以我粘贴了整个代码。我正在测试测试类并得到错误,如“引起:org.springframework.beans.factory.BeanCreationException:创建带名称的bean时出错” userRepository':init方法的调用失败;嵌套异常是java.lang.IllegalArgumentException:无法为方法public abstract org.home.mysystem.entity.User org.home.mysystem.repository.UserRepository.findOne(java.lang)创建查询.String)!没有为User类型找到属性findOne!“。请有人帮帮我

Role.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;
    }

}

Task.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() {

    }

}

RoleRepository.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);

    }

UserRepository.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);

}

TaskService.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);
        }
    }

UserService.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);
    }
}

MyApplicationTest.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 个答案:

答案 0 :(得分:1)

问题在于Spring正在期待你提供的其他东西。

默认情况下,

findOne定义为使用ID(主键)加载实体。所以它期望longLong(据我所知)。它采用给定参数的名称(电子邮件),并且正在搜索具有该名称的ID,并且根本不会加起来。

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

示例1

要搜索的示例字段:email

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

示例2

要搜索的示例字段:username

存储库中的方法: User findByUsername(String username)

我希望这有帮助!