【问题标题】:No property [MethodName] found for type [EntityName] while executing custom jpa repository method执行自定义 jpa 存储库方法时未找到类型 [EntityName] 的属性 [MethodName]
【发布时间】:2021-08-16 13:35:35
【问题描述】:

我需要在 ServiceImpl 中准备一个查询,因为根据某些逻辑,查询可能会有所不同。的列。所以我决定准备一个自定义 JPA 存储库,但遇到了一些错误。

在此之前,为了满足我的要求,我尝试了这个 approach ,请检查。但我认为 JPA 不允许这样。所以我尝试了自定义 JPA 存储库并收到错误。

**



 Entity class


  @SuppressWarnings("serial")
 
  @Entity
  
  @Getter
  
  @Setter
  
  @Table(name = "REASON_CODE_REPORT",schema="automation") 
  @IdClass(ErrorCodeReportKeys.class)
  public class ErrorCodeReportEntity
  {
        @Id
        private String smsc;
        
        @Id
        private String userid;
        
        @Id
        private String smsc_userid;
        
        @Id
        private String operator;
        
        @Id
        private String circle;
        
        @Id
        private Date log_date;
        
        @Id
        private Integer log_hour;
        
        @Id
        private Integer log_min;
        
        @Id
        private  Integer vf_reason_code;
        
        @Id
        private  Integer smsc_reason_code;

        private Integer count;
        
        private Timestamp create_date;

  }

**

服务实现

@Override
    public List<Object[]> errorCodeDefaultSummary(ErrorCodeReportDTO errorCodeReportDTO) {
 
          String finalQuery="select smsc,userid from ErrorCodeReportEntity where log_date='2021-05-27'";
      List<Object[]> result =   errorCodeRepo.presentDaySummarySmscWise(finalQuery);
      
     
        return result;
}

自定义 JPA 接口

public interface ErrorCodeCustom {

      List<Object[]> presentDaySummarySmscWise(String query);
        
}

ErrorCodeCustomImpl 的实现

public class ErrorCodeCustomImpl implements ErrorCodeCustom{

    @Autowired
    private EntityManager entityManager;
    
    
    @SuppressWarnings("unchecked")
    @Override
    public List<Object[]> presentDaySummarySmscWise(String query) {
        
     final  String finalQuery=query.toString();
        
       List<Object[]>  result= entityManager.createQuery(finalQuery).getResultList();
        
        
        
        return result;
    }

}

实现我们的 CustomRepository 的最终 Jpa 存储库

@Repository
public interface ErrorCodeRepository extends JpaRepository<ErrorCodeReportEntity, ErrorCodeReportKeys>,ErrorCodeCustom
{
}

我不知道为什么会出现以下错误

Caused by: java.lang.IllegalArgumentException: Failed to create query for method public abstract java.util.List com.valuefirst.repository.ErrorCodeCustom.presentDaySummarySmscWise(java.lang.String)! No property presentDaySummarySmscWise found for type ErrorCodeReportEntity!

Caused by: org.springframework.data.mapping.PropertyReferenceException: No property presentDaySummarySmscWise found for type ErrorCodeReportEntity!

【问题讨论】:

    标签: java spring spring-boot hibernate spring-data-jpa


    【解决方案1】:

    Spring 无法找到您的实现 ErrorCodeCustomImpl。你应该注释它@Component。

    所以出现异常是因为 ErrorCodeRepository 扩展了 ErrorCodeCustom 并且您的方法名称生成的查询尝试使用名为 presentDaySummarySmscWise 的属性。一旦你的 ErrorCodeCustomImpl 将被 Spring 注入,错误就会消失。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-12-06
      • 2017-05-18
      • 2020-03-22
      • 2021-04-01
      • 2016-08-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多