【问题标题】:Error - between date not working in Jparepository错误 - 在 Jparepository 中不起作用的日期之间
【发布时间】:2020-05-14 05:35:05
【问题描述】:

在不工作的 JPA 之间。

模态类:

public class Record extends AbstractTimestampEntity{

string id;
string senderId;
string reciverId;
string status;
@Column(name = "txn_Date", columnDefinition = "DATE")
Date txnDate;

}

JPA 查询:

findByTxnDateBetweenAndSenderIdOrReciverIdAndStatus(Date from,Date to,String serderId,String reciverId,String status);

即使我从和到日期发送过滤器也没有在查询中应用。

我在mysql查询中尝试过同样的查询

select * from record where  txn_date between "2020-01-09" and "2020-05-09" and sender_id = "98bd543e-942d-4725-b9bc-32c99eb089ae" or reciver_id = "98bd543e-942d-4725-b9bc-32c99eb089ae" and status = "COMPLETED";

在这里我得到了预期的结果。但是在 JPA 中没有得到

【问题讨论】:

  • 您能描述一下您的And-Or sql 查询条件吗?
  • 您遇到的错误是什么?
  • 我没有看到任何错误。

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


【解决方案1】:

通过方法名组合多个 And 和 Or 很困难,并且需要重复字段。您当前的方法代表您想要的其他东西。

对于这种情况A and (B or C),您必须使用类似(A and B) or (A and C) 的方法进行查询。因为您的查询会更复杂。 .

最好使用带有@Query 注解的JPQL。假设你想要像

这样的条件

(txn_date and sender_id) or (reciver_id and status)

那你就可以这样了

@Query("select r from Record r where ((r.txnDate BETWEEN ?1 AND ?2) and senderId = ?3) or (reciverId = ?4 and status = ?5)")
List<Record> findByTxnDateBetweenAndSenderIdOrReciverIdAndStatus(Date from,Date to,String serderId,String reciverId,String status);
   User findByEmailAddress(String emailAddress);

【讨论】:

  • 我能够得到正确的细节。这里我看到了一个问题。它在时间戳范围内加载是否有任何解决方案
  • it is loading with in time stamp range这部分我没看懂,你的问题能详细说明一下
  • 1) @Column(name = "transaction_date", columnDefinition = "DATE")
  • 如果您需要将 TimeStamp 转换为 Date 请遵循此答案stackoverflow.com/a/61792189/4207306
  • 当您在查询(to,from) 中发送日期时,也尝试设置时间,您发送哪些数据,它将查询您发送时间的日期。请参阅此处如何更改日期对象时间stackoverflow.com/questions/10308356/…
【解决方案2】:

尝试使用@Temporal 注解:-

public class Record{

string id;
string senderId;
string reciverId;
string status;
@Temporal(TemporalType.TIMESTAMP)
Date txnDate;
}

【讨论】:

    【解决方案3】:

    尽管这是一个模棱两可的问题,没有关于错误消息的足够详细信息,但请尝试使用 java.sql.Timestamp 数据类型作为您的字段类型并发出 columnDefinition。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多