具有多个字段和日期的 Spring Data Paging and Sorting Repository
Posted
技术标签:
【中文标题】具有多个字段和日期的 Spring Data Paging and Sorting Repository【英文标题】:Spring Data Paging And Sorting Repository with multiple fields and date between 【发布时间】:2020-05-13 11:21:33 【问题描述】:下面是我的视图实体vw_invoice_report
@Entity
@Table(name = "vw_invoice_report")
class Report implements Serializable
private static final long serialVersionUID = 1L;
@Id
@Column(name = "id")
private Long id;
@Column(name = "sender_name")
private String sender;
@Column(name = "client_name")
private String client;
@Column(name = "submit_date")
@Temporal(TemporalType.TIMESTAMP)
private Date submitDate;
@Column(name = "invoice_num")
private String invoiceNum;
//getters
我正在尝试实现findByClientAndLawfirmAndSubmitDateBetween
以使用client,sender, submitDate
字段查询视图
public interface ReportRepository extends PagingAndSortingRepository<Report, Long>
public List<Report> findByClientAndLawfirmAndSubmitDateBetween(String client, String sender, Date start, Date stop, Pageable pageable)
当我尝试调用上述方法时,出现以下错误
org.hibernate.QueryException: could not resolve property: client of: com.xyz.reports.Report
[select generatedAlias0 from com.xyz.reports.Report as generatedAlias0 where ( ( generatedAlias0.client=:param0 ) and ( generatedAlias0.sender=:param1 ) ) and ( generatedAlias0.submitDate between :param2 and :param3 )]; nested exception is java.lang.IllegalArgumentException: org.hibernate.QueryException: could not resolve property: client of: com.xyz.reports.Report [select generatedAlias0 from com.xyz.reports.Report as generatedAlias0 where ( ( generatedAlias0.client=:param0 ) and ( generatedAlias0.sender=:param1 ) ) and ( generatedAlias0.submitDate between :param2 and :param3 )] 34178348919273527
但是,如果我用Query
注释将上面的查询注释为下面的工作正常
@Query(value="SELECT id, sender_name, client_name, submit_date, invoice_num FROM vw_invoice_report WHERE client_name = ?1 AND sender_name = ?2 AND submit_date BETWEEN ?3 AND ?4 ORDER BY submit_date desc",
nativeQuery = true)`
有没有办法让findByClientAndLawfirmAndSubmitDateBetween()
在没有@Query
的情况下工作
【问题讨论】:
您的实体中的Lawfirm
在哪里??
我的错,重构很糟糕,lawfirm
是Report
实体中的sender
字段
是不是导致异常???你解决了吗???
@RanPaul 您的PagingAndSortingRepository
的标识符类型是Integer
,而@Id
属性是Long
。你能检查一下这是否会导致问题吗?
@Daniel 我把它改成了Long
仍然面临这个问题
【参考方案1】:
要么
将属性 sender
重命名为 lawFirm
@Column(name = "sender_name")
private String lawfirm;
或
将您的存储库方法重命名为:
findByClientAndSenderAndSubmitDateBetween
【讨论】:
以上是关于具有多个字段和日期的 Spring Data Paging and Sorting Repository的主要内容,如果未能解决你的问题,请参考以下文章
spring-data-elasticsearch 在多个索引中搜索特定字段
多个数据库使用相同的域模型和 Spring Boot 和 Spring Data JPA
使用 Spring Boot WebFlux、Spring Data MongoDB Reactive 和 ReactiveMongoRepository 更新 1 个或多个特定字段 MongoDB