[英]JPQL query for time difference
I have two dates in the entity REPORTED_TIME and CREATION_TIME 我在实体REPORTED_TIME和CREATION_TIME有两个日期
How can I create JPQL to fetch recored where the time difference between(CREATION_TIME-REPORTED_TIME) is only 12 hr. 我如何创建JPQL以获取重新记录的内容,其中(CREATION_TIME-REPORTED_TIME)之间的时间差仅为12小时。
I don't want any native query syntax tas we are using both Oracle and Postgres. 我不希望我们同时使用Oracle和Postgres的任何本地查询语法。
Can someone give a query for the same in Oracle? 有人可以在Oracle中查询相同的内容吗?
/**
* The persistent class for the IR_TB_INCIDENT_HDR database table.
*
*/
@Entity
@Table(name="IR_TB_INCIDENT_HDR")
public class IncidentHdr implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@SequenceGenerator(name="IR_TB_INCIDENT_HDR_INCIDENTID_GENERATOR", sequenceName="INCIDENT_SEQ")
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="IR_TB_INCIDENT_HDR_INCIDENTID_GENERATOR")
@Column(name="INCIDENT_ID")
private long incidentId;
@Temporal(TemporalType.DATE)
@Column(name="CREATED_TIME")
private Date createdTime;
@Temporal(TemporalType.DATE)
@Column(name="REPORTED_TIME")
private Date reportedTime;
public IncidentHdr() {
}
//Setter getter
}
The JPA spec doesn't contains a method for difference between date/time. JPA规范不包含区分日期/时间的方法。 But you can call functions, a little bit better than native queries. 但是您可以调用函数,这比本机查询好一点。 You must ensure that the datediff function is available. 您必须确保datediff函数可用。
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<IncidentHdr> query = cb.createQuery(IncidentHdr.class);
Root<IncidentHdr> root = query.from(IncidentHdr.class);
Predicate max12Hour = cb
.lessThanOrEqualTo(
cb.function("datediff", Long.class, cb.literal("HOUR"), root.get(IncidentHdr_.createdTime),
root.get(IncidentHdr_.reportedTime)),
12L);
query.select(root)
.where(max12Hour);
return entityManager.createQuery(query).getSingleResult();
I hope it will help you. 希望对您有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.