Spring JPA query results are multiple, but the return value is one, and the program does not report an error.

public interface UserRepository extends MongoRepository<User, Long> {
    User findByUsername(String username);
}

the above code uses the function of Spring JPA to find users according to their names, but multiple data can be queried in the database according to their names zong , and the return value of the findByUsername method is User, not List < User >. At this time, the expected result should be an error, but the program does not report an error, but returns the first item of multiple data.

excuse me, what is the reason for this? why did you report it correctly?

Sep.16,2021

User, so only return one
. If you change the declaration to List < User >, you can return multiple
. This is the advantage of JPA. How can you expect him to report a mistake

?
Menu