We have a repository with standard features:

save findOne exists findAll count delete deleteAll 

I need to add a new, login search. And I can not figure out how to create it correctly. I looked on the Internet, but understood little. Did, as written here http://devcolibri.com/4149 Here is the essence:

 package ua.samuliak.messenger.entity; import org.hibernate.annotations.GenericGenerator; import javax.persistence.*; @Entity @Table(name = "\"user\"") public class User { @Id @GeneratedValue(generator = "increment") @GenericGenerator(name = "increment", strategy = "increment") private long id; @Column(nullable = false, length = 20) private String login; @Column(nullable = false, length = 20) private String password; @Column(length = 2) private String country; @ManyToOne(fetch = FetchType.EAGER, cascade = {CascadeType.MERGE, CascadeType.PERSIST}) @JoinColumn(name = "room_id") private Room room; public Room getRoom() { return room; } public void setRoom(Room room) { this.room = room; } public User() {} public long getId() { return id; } public void setId(long id) { this.id = id; } public String getLogin() { return login; } public void setLogin(String login) { this.login = login; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getCountry() { return country; } public void setCountry(String country) { this.country = country; } } 

Part of the code from the controller:

 ..... @RequestMapping(value = "/user/{name}", method = RequestMethod.GET) @ResponseBody public User getUserByName(@PathVariable("name") String userLogin){ return userService.getByLogin(userLogin); } ..... 

And repository

 package ua.samuliak.messenger.repository; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; import ua.samuliak.messenger.entity.User; public interface UserRepository extends JpaRepository<User, Long>{ @Query("FROM User WHERE login = :name ") User findByName(String name); } 

Help with the repository, please! How to make a search query by name correctly, what would the entity return?

I put it a little wrong, there is no error as such, just the request itself is underlined (I lay down the screen) and writes "FROM" unexpected. And when requesting a server (see the code with the controller), it does not issue the user by login, but writes such an error (screen No. 2). enter image description here enter image description here

New problem

 31-May-2016 11:08:59.847 SEVERE [http-apr-8080-exec-35] org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service() for servlet [dispatcher] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.InvalidDataAccessApiUsageException: Name for parameter binding must not be null or empty! For named parameters you need to use @Param for query method parameters on Java versions < 8.; nested exception is java.lang.IllegalArgumentException: Name for parameter binding must not be null or empty! For named parameters you need to use @Param for query method parameters on Java versions < 8.] with root cause java.lang.IllegalArgumentException: Name for parameter binding must not be null or empty! For named parameters you need to use @Param for query method parameters on Java versions < 8. 
  • and what actually doesn't work for you? - Victor
  • quarrel over the repository request ( - Samuliak
  • Please add some error rates. then we will definitely help you :) - Victor
  • one
    You have a problem not with the repository, but with the controller, what the spring kindly informs you about, most likely you have a second controller based on a similar path, the spring does not know which one to choose and swears - check. in particular, the difference between the url in your getUserById method and the getUserByName method - Victor is not obvious for the spring.
  • one
    add @Param ("name") to the parameter of the method in the repository, you get something like User findByName (@Param ("name") String name), or debazhe and make sure that the spring correctly pulls the name out of @PathVariable - Victor

1 answer 1

You have a problem not with the repository, but with the controller, about which the spring kindly informs you. You have a second controller based on a similar path, the spring does not know which one to choose and swears.

in particular, for springing, the difference between the url in your method is not obvious.

  • getUserById

    and method

  • getUserByName