I have two dates in the entity REPORTED_TIME and CREATION_TIME
How can I create JPQL to fetch recored where the time difference between(CREATION_TIME-REPORTED_TIME) is only 12 hr.
I don't want any native query syntax tas we are using both Oracle and Postgres.
Can someone give a query for the same in 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. But you can call functions, a little bit better than native queries. You must ensure that the datediff function is available.
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.