【发布时间】: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