添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I created a login user using controller, user, repository and service in my project. In controller, if email id and password are not null then login otherwise throw an exception "User Not Exist". I am getting the bellow error, can you help me to fix this issue?

Error:

Error creating bean with name 'registrationRepository': FactoryBean threw exception on object creation; nested exception is java.lang.IllegalArgumentException: Failed to create query for method public abstract com.adventure.network.model.User com.adventure.network.repository.RegistrationRepository.findByEmailIdAndByPassword(java.lang.String,java.lang.String)! No property byPassword found for type User!

Controller:

@RestController
public class RegistrationController {
    @Autowired
    private RegistrationService service;
    @PostMapping("/login")
    public User loginUser(@RequestBody User user) throws Exception {
        String tempEmailId = user.getEmailId();
        String tempPassword = user.getPassword();
        User userObject = null;
        if(tempEmailId!=null && tempPassword!=null) {
            userObject = service.fetchUserByEmailIdByPassword(tempEmailId, tempPassword);   
        if(userObject == null) {
            throw new Exception("User is not exict");
        return userObject;

Service:

@Service
public class RegistrationService {
    @Autowired 
    private RegistrationRepository repo;
    public User fetchUserByEmailIdByPassword(String email, String password) {
        return repo.findByEmailIdAndByPassword(email, password);

Repository:

public interface RegistrationRepository extends JpaRepository<User, Integer> {
    public User findByEmailIdAndByPassword(String emailId, String password);

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.adventure</groupId>
    <artifactId>network</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>network</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>11</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>
                Oh,  Thanx it got fixed after changing findByEmailIdAnaByPassword to findByEmailIdAndPassword. The method can be any name why my method name was getting error?
– ehsan hosseini
                Sep 14, 2020 at 2:30
                Not in the repository. Spring parses method name of repository class to understand which properties of User class to use for search. That is how JPA repository works in spring boot
– Ivan
                Sep 14, 2020 at 2:31

As per your error message No property byPassword found for type User!, it is looking for the property named byPassword you need to change to andPassword in the repository as shown below.

public interface RegistrationRepository extends JpaRepository<User, Integer> {
    public User findByEmailIdAndPassword(String emailId, String password);

Spring will parse every property mentioned after by, you need by only at the start of the method

You can also change the name to be more readable as

public interface RegistrationRepository extends JpaRepository<User, Integer> {
    public User findUserByEmailIdAndPassword(String emailId, String password);
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.