我在实体REPORTED_TIME和CREATION_TIME
中有两个日期如何在(CREATION_TIME-REPORTED_TIME)之间的时差仅为12小时的情况下创建用于获取recored的JPQL。
我不想要任何本机查询语法,我们同时使用Oracle和Postgres。
有人可以在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
}
答案 0 :(得分:0)
JPA规范不包含日期/时间差异的方法。但是你可以调用函数,比原生查询好一点。您必须确保datediff function可用。
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();
我希望它会对你有所帮助。