简体   繁体   English

JpaRepository 中自定义本机查询,自定义名称抛出 QueryCreationException

[英]Custom Native Query in JpaRepository with a custom name throwing QueryCreationException

I am stuck in a very trivial issue for 2 days.我在一个非常微不足道的问题上停留了 2 天。 I am creating a spring boot project with Rest endpoints.我正在创建一个带有 Rest 端点的 spring 引导项目。 There are 2 entity classes - Employee and Address with OneToMany mapping with 1 employee having a list of addresses.有 2 个实体类 - Employee 和 Address 具有 OneToMany 映射,其中 1 名员工具有地址列表。 I have created an EmployeeRepository class that implements JpaRepository for performing employee related transactions.我创建了一个 EmployeeRepository class,它实现了 JpaRepository 以执行与员工相关的交易。 So there is a requirement of fetching an employee by the address id, so as per my understanding, if we have to create our own custom query which is not provided by JpaRepository, we need to use @Query annotation above our query method.所以需要通过地址 id 获取员工,所以根据我的理解,如果我们必须创建自己的自定义查询,而 JpaRepository 没有提供,我们需要在查询方法上方使用 @Query 注释。 That method can be named anything - I have named it - getByAddress .该方法可以命名为任何名称 - 我将其命名为 - getByAddress I am facing problem here.我在这里面临问题。 Employee class has List of Addresses like -员工 class 的地址列表如下 -

@OneToMany
    @JoinColumn(name = "eId", referencedColumnName = "empId")
    List<Address> address;

Now whenever I am trying to fetch the employee by id by passing Integer address id in the Url, it says, expecting an address in URL, I am suspecting this is because I have named the method - getByAddress.现在每当我试图通过在 Url 中传递 Integer 地址 id 来获取员工时,它说,期待 URL 中的地址,我怀疑这是因为我已经命名了方法 - getByAddress。 So I changed the name to getByAddrId .所以我将名称更改为getByAddrId

This throws exception -这会引发异常-

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'employeeRepository' defined in com.shweta.rest.repository.EmployeeRepository defined in @EnableJpaRepositories declared on SpringRestApplication: Invocation of init method failed; org.springframework.beans.factory.BeanCreationException:在 com.shweta.rest.repository.EmployeeRepository 中定义的名为“employeeRepository”的 bean 在 SpringRestApplication 上声明的 @EnableJpaRepositories 中定义时出错:Invocation of init 方法失败; nested exception is org.springframework.data.repository.query.QueryCreationException: Could not create query for public abstract com.shweta.rest.entity.Employee com.shweta.rest.repository.EmployeeRepository.getByAddrId(int): Reason.嵌套异常是 org.springframework.data.repository.query.QueryCreationException:无法为公共摘要 com.shweta.rest.entity.Employee com.shweta.rest.repository.EmployeeRepository.getByAddr 创建查询: Failed to create query for method public abstract com.shweta.rest.entity.Employee com.shweta.rest.repository.EmployeeRepository;getByAddrId(int).无法为方法 public abstract com.shweta.rest.entity.Employee com.shweta.rest.repository.EmployeeRepository;getByAddrId(int) 创建查询。 No property 'addrId' found for type 'Employee'.: nested exception is java.lang.IllegalArgumentException.找不到类型“Employee”的属性“addrId”。嵌套异常是 java.lang.IllegalArgumentException。 Failed to create query for method public abstract com.shweta.rest.entity.Employee com.shweta.rest.repository.EmployeeRepository.getByAddrId(int)!无法为方法 public abstract com.shweta.rest.entity.Employee com.shweta.rest.repository.EmployeeRepository.getByAddrId(int) 创建查询! No property 'addrId' found for type 'Employee'!找不到类型“员工”的属性“addrId”!

Caused by: org.springframework.data.repository.query.QueryCreationException: Could not create query for public abstract com.shweta.rest.entity.Employee com.shweta.rest.repository.EmployeeRepository.getByAddrId(int): Reason.由以下原因引起:org.springframework.data.repository.query.QueryCreationException:无法为公共摘要 com.shweta.rest.entity.Employee com.shweta.rest.repository.EmployeeRepository.getByAddr 创建查询: Failed to create query for method public abstract com.shweta.rest.entity.Employee com.shweta.rest.repository.EmployeeRepository;getByAddrId(int).无法为方法 public abstract com.shweta.rest.entity.Employee com.shweta.rest.repository.EmployeeRepository;getByAddrId(int) 创建查询。 No property 'addrId' found for type 'Employee'.: nested exception is java.lang.IllegalArgumentException.找不到类型“Employee”的属性“addrId”。嵌套异常是 java.lang.IllegalArgumentException。 Failed to create query for method public abstract com.shweta.rest.entity.Employee com.shweta.rest.repository.EmployeeRepository.getByAddrId(int)!无法为方法 public abstract com.shweta.rest.entity.Employee com.shweta.rest.repository.EmployeeRepository.getByAddrId(int) 创建查询! No property 'addrId' found for type 'Employee'!找不到类型“员工”的属性“addrId”!

I am highly confused now, we can give custom query method names then why is it treating my method name as a field in Employee class which its of course unable to find as there is no field like getByAddrId in my Employee class.我现在非常困惑,我们可以提供自定义查询方法名称,那么为什么它将我的方法名称视为 Employee class 中的一个字段,它当然无法找到,因为我的 Employee class 中没有像 getByAddrId 这样的字段。

Look at my classes below:看看我下面的课程:

My employee class:我的员工 class:

在此处输入图像描述

My Address class :我的地址 class :

在此处输入图像描述

Controller snippet: Controller 片段:

/**
 To get Employee by address id
 */
@GetMapping("/getEmployeeByAddressId/{id}")
public ResponseEntity<Employee> getEmployeebyAddressId(@PathVariable("id") int id){
    return new ResponseEntity<Employee>(empService.getEmployeebyAddressId(id), HttpStatus.FOUND);
}

My Service class method that's making call to custom query method from EmployeeRepository:我的服务 class方法从 EmployeeRepository 调用自定义查询方法:

 @Override 
  public Employee getEmployeebyAddressId(int id) 
  { 
      Employee emp = employeeRepository.getByAddrId(id); 
      return emp; 
  }

My JpaRepository class that has the custom method with custom query (query works fine in MySQL):我的 JpaRepository class具有带有自定义查询的自定义方法(查询在 MySQL 中工作正常):

@Repository

public interface EmployeeRepository extends JpaRepository<Employee, Integer> { public interface EmployeeRepository extends JpaRepository<Employee, Integer> {

  //Native Query
  @Query(
  name="select * from tb_employee where emp_id = (select e_id from tb_address where addr_id=?1 ", nativeQuery = true) 
  Employee getByAddrId(int id);
  

Please help me, I am not able to move ahead because of this blocker, my requirement is to create a custom query method to run any complex query请帮助我,因为这个障碍我无法继续前进,我的要求是创建一个自定义查询方法来运行任何复杂的查询

In the spring jpa repository some methods are reserved.在 spring jpa 存储库中保留了一些方法。

Can you please try changing your repository method to new name like您能否尝试将存储库方法更改为新名称,例如

getMyEmployee获取我的员工

Also i see the query is not closed with ).我还看到查询未关闭)。

I found the issue:我发现了问题:

In EmployeeRepository.java, instead of giving 'name' in @Query, 'value' attribute was to be given which was causing all the issues.在 EmployeeRepository.java 中,不是在 @Query 中给出“名称”,而是给出“值”属性,这导致了所有问题。 After I fixed it to value, it worked fine, so below snippet is correct one在我将它固定为值后,它工作正常,所以下面的片段是正确的

@Query(
  value="select * from tb_employee where emp_id = (select e_id from tb_address where addr_id=?1) ", nativeQuery = true) 
  Employee getByAddrId(int id);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM