如何解决带有下划线变量的 Spring Boot findBy 方法

Kau*_*sam 5 jpa spring-data-jpa spring-boot

当我运行以下项目时,我收到以下错误。我该如何修复它?

引起原因:java.lang.IllegalArgumentException:无法为方法公共抽象com.example.pharmanic.model.Rdhs_Hospital_Current_Stock com.example.pharmanic.repositories.Rdhs_Hospital_Current_StockRepository.findBysr_no(java.lang.String)创建查询!未找到 Rdhs_Hospital_Current_Stock 类型的属性 sr!

这是我的Rdhs_Hospital_Current_Stock模型课。

@Entity
@Data
@Table(name = "Rdhs_Hospital_Current_Stock")
public class Rdhs_Hospital_Current_Stock {
    @Id
    private Long batchId;
    private int quantity;
    private String expiredate;

    @ManyToOne
    private Hospital_By_Rdhs hospital_by_rdhs;


    @ManyToOne
    @JoinColumn(name = "sr_no", nullable = false, referencedColumnName = "sr_no")
    private Medicine medicine;


}
Run Code Online (Sandbox Code Playgroud)

sr_no是表的外键Medicine

这是我的Medicine实体:

@Data
@Entity
public class Medicine {
    private @Id String sr_no;

    private String name;
    private String side_effect;
    private String description;

    public Medicine() {
    }

    public Medicine(String sr_no, String name, String side_effect, String description) {
        this.sr_no = sr_no;
        this.name = name;
        this.side_effect = side_effect;
        this.description = description;
    }
  }
Run Code Online (Sandbox Code Playgroud)

当我sr_no与我的findBy()函数一起使用时:

 @GetMapping("/rhstock/{id}")
    ResponseEntity<?> getMedicine(@PathVariable String id){
        Optional<Rdhs_Hospital_Current_Stock> rdhs_hospital_current_stock = Optional.ofNullable(rdhs_hospital_current_stockRepository.findBysr_no(id));
         return rdhs_hospital_current_stock.map(response->ResponseEntity.ok().body(response)).orElse(new ResponseEntity<>(HttpStatus.NOT_FOUND));
    }
Run Code Online (Sandbox Code Playgroud)

这是我的存储库:

public interface Rdhs_Hospital_Current_StockRepository extends JpaRepository<Rdhs_Hospital_Current_Stock,Long> {
    Rdhs_Hospital_Current_Stock findBysr_no(String id);

}
Run Code Online (Sandbox Code Playgroud)

Kau*_*sam 0

我解决了这个错误。我在存储库接口和控制器类中进行更改,如下所示

存储库接口 -:

@Query(value="select * from Rdhs_Hospital_Current_Stock h where h.sr_no = :sr_no",nativeQuery=true)
 List<Rdhs_Hospital_Current_Stock> findBySr_no(@Param("sr_no")String sr_no);
Run Code Online (Sandbox Code Playgroud)

控制器类-:

 @RequestMapping(value = "/rhstocksr/{sr_no}", method = RequestMethod.GET)
    List<Rdhs_Hospital_Current_Stock> getBatchByMedicine(@PathVariable("sr_no") String sr_no) {
        return rdhs_hospital_current_stockRepository.findBySr_no(sr_no);

    }
Run Code Online (Sandbox Code Playgroud)