Filed to create query for method public abstract

huangapple go评论77阅读模式
英文:

Filed to create query for method public abstract

问题

我在项目中使用控制器用户仓库和服务创建了一个登录用户在控制器中如果电子邮件和密码不为空则登录否则抛出异常 "用户不存在"我遇到了以下错误请帮我解决这个问题

错误信息

    创建bean时出错bean名称为 'registrationRepository':FactoryBean 在创建对象时抛出异常嵌套异常是 java.lang.IllegalArgumentException: 无法为类型 User 找到属性 byPassword 的方法 public abstract com.adventure.network.model.User com.adventure.network.repository.RegistrationRepository.findByEmailIdAndByPassword(java.lang.String,java.lang.String) 的查询

控制器代码

    @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("用户不存在");
    		}
    		return userObject;
    	}
    }

服务代码

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

仓库代码

    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>
英文:

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 &#39;registrationRepository&#39;: 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(&quot;/login&quot;)
public User loginUser(@RequestBody User user) throws Exception {
String tempEmailId = user.getEmailId();
String tempPassword = user.getPassword();
User userObject = null;
if(tempEmailId!=null &amp;&amp; tempPassword!=null) {
userObject = service.fetchUserByEmailIdByPassword(tempEmailId, tempPassword);	
}
if(userObject == null) {
throw new Exception(&quot;User is not exict&quot;);
}
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&lt;User, Integer&gt; {
public User findByEmailIdAndByPassword(String emailId, String password);
} 

pom.xml

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
&lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
&lt;parent&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
&lt;version&gt;2.3.3.RELEASE&lt;/version&gt;
&lt;relativePath/&gt; &lt;!-- lookup parent from repository --&gt;
&lt;/parent&gt;
&lt;groupId&gt;com.adventure&lt;/groupId&gt;
&lt;artifactId&gt;network&lt;/artifactId&gt;
&lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
&lt;name&gt;network&lt;/name&gt;
&lt;description&gt;Demo project for Spring Boot&lt;/description&gt;
&lt;properties&gt;
&lt;java.version&gt;11&lt;/java.version&gt;
&lt;/properties&gt;
&lt;dependencies&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-data-jpa&lt;/artifactId&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;com.h2database&lt;/groupId&gt;
&lt;artifactId&gt;h2&lt;/artifactId&gt;
&lt;scope&gt;runtime&lt;/scope&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
&lt;scope&gt;test&lt;/scope&gt;
&lt;exclusions&gt;
&lt;exclusion&gt;
&lt;groupId&gt;org.junit.vintage&lt;/groupId&gt;
&lt;artifactId&gt;junit-vintage-engine&lt;/artifactId&gt;
&lt;/exclusion&gt;
&lt;/exclusions&gt;
&lt;/dependency&gt;
&lt;/dependencies&gt;
&lt;build&gt;
&lt;plugins&gt;
&lt;plugin&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
&lt;/plugin&gt;
&lt;/plugins&gt;
&lt;/build&gt;
&lt;/project&gt;

答案1

得分: 1

根据您的错误信息 No property byPassword found for type User!,它正在寻找名为 byPassword 的属性,您需要在存储库中将其更改为 andPassword,如下所示:

public interface RegistrationRepository extends JpaRepository<User, Integer> {

    public User findByEmailIdAndPassword(String emailId, String password);
}

Spring 将解析在 by 后面提到的每个属性,您只需要在方法开头使用 by。

您还可以将名称更改为更易读的形式:

public interface RegistrationRepository extends JpaRepository<User, Integer> {

    public User findUserByEmailIdAndPassword(String emailId, String password);
}
英文:

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&lt;User, Integer&gt; {
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&lt;User, Integer&gt; {
public User findUserByEmailIdAndPassword(String emailId, String password);
}

huangapple
  • 本文由 发表于 2020年9月14日 08:18:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/63876730.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定