0

Problem

Invoking repository.save(user); where repository is an instance of UserRepository and user.toString is User[id=0, userName='asd', password='asd'], my controller gives the following error:

nested exception is java.lang.IllegalArgumentException: Invoked method public abstract java.lang.Object org.springframework.data.repository.CrudRepository.save(java.lang.Object) is no accessor method!] with root cause

java.lang.IllegalArgumentException: Invoked method public abstract java.lang.Object org.springframework.data.repository.CrudRepository.save(java.lang.Object) is no accessor method!

Details

I know repository.save(new User("user1", "pass1")); works because I tested my repository using:

public CommandLineRunner demo(UserRepository repository) {
        return (args) -> {
                // save a couple of users
                repository.save(new User("user1", "pass1"));
                repository.save(new User("user2", "pass2"));
                }
}

Here's the full Request mapping of the page:

RequestMapping(value = "/register", method = RequestMethod.GET)
public String showRegistrationForm(WebRequest request, Model model) {
        User user = new User();
        model.addAttribute("user", user);
        return "registrations/register";
}

@RequestMapping(value = "/register", method = RequestMethod.POST)
public String registerUserAccount(WebRequest request,
                                        @ModelAttribute("user") @Valid User user,
                                        BindingResult result,
                                        UserRepository repository) {
        if (!result.hasErrors()) {

          System.out.println(user);
                repository.save(user);
        }
        return "redirect:/";

}

And here's the UserRepository:

import java.util.List;

import org.springframework.data.repository.CrudRepository;

public interface UserRepository extends CrudRepository<User, Long> {

    List<User> findByUserName(String UserName);
}

And here's the User @Entity:

@Entity
public class User {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private long id;
    private String userName;
    private String password;

    protected User() {}

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

    // standard getters and setters

    @Override
    public String toString() {
        return String.format(
                "User[id=%d, userName='%s', password='%s']",
                id, userName, password);
    }

}

1 Answer 1

2

It seems the UserRepository repository in code of Controller should not be used as parameter: ...

public String registerUserAccount(WebRequest request,
                                        @ModelAttribute("user") @Valid User user,
                                        BindingResult result,
                                        UserRepository repository)

...

Can you just use @Autowired it like:

@Autowired 
 private UserRepository repository;

And then try again?!

Hope this help.

Sign up to request clarification or add additional context in comments.

2 Comments

It works! You beautiful, beautiful man! Now please explain why it works!
Good to know that it works for you. Some details are: UserRepository is extended from CrudRepository. So, the bean will be created automatically and it is used as a dependency in Controller via @Autowired. Spring does not regconized the dependency as a parameter in the request. Most of the time, I just use the model in it. Hope it help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.