我使用时间戳列选择的jpql " where子句" 不会"过滤"正如所料。
它似乎对" date"时间戳的一部分,但忽略了"时间"部分。
table1.tmstmp列定义为:" TMSTMP TIMESTAMP NOT NULL"
table1实体包含以下命名查询...
@NamedQuery(name = "table1.getUpdTime", query = "SELECT e FROM table1 e
WHERE e.tmstmp >= :tmstmp order by e.tmstmp asc")
tmstmp属性定义如下......
@Column(name = "TMSTMP", nullable = false)
private Timestamp tmstmp;
像这样使用......
TypedQuery<EsEvnt> query = em.createNamedQuery("table1.getUpdTime",
table1.class).setParameter("tmstmp", tmstmp);
例如,当上面的Timestamp变量(&#34; tmstmp&#34;)等于&#34; 2016-06-30T09:28:33.247-04:00&#34;时,它返回全部记录日期部分为:2016-06-30。
有什么想法吗?
我想依赖&#34; where子句&#34;明确选择使用整个时间戳。
谢谢!
更新
我使用&#34; CAST&#34;修改了我的查询(似乎需要本机查询)。功能,如下所示,它似乎一致地工作......
//...note, date is in ISO format...
LocalDateTime ldt = LocalDateTime.parse(tmstmpString, DateTimeFormatter.ISO_DATE_TIME);
Timestamp tmstmp = Timestamp.valueOf(ldt);
-
-
-
// (see "native query", coded below)...
Query query = em.createNativeQuery("SELECT * FROM table1 e WHERE e.tmstmp >= CAST(?1 as TIMESTAMP)", Table1.class)
.setParameter(1, tmstmp);
List<Table1> resultList = query.getResultList();
谢谢! :)
WebLogic 12.1.3
DB2
Java 8
EclipseLink 2.1
答案 0 :(得分:0)
Below, is what worked for me...
Assuming...
//...note, date is in ISO format...
LocalDateTime ldt = LocalDateTime.parse(tmstmpString, DateTimeFormatter.ISO_DATE_TIME);
Timestamp tmstmp = Timestamp.valueOf(ldt);
Here's one way...
Query query = entityManager.createNativeQuery("SELECT * FROM table1 e WHERE e.tmstmp >= CAST(?1 as TIMESTAMP)", Table1.class)
.setParameter(1, tmstmp);
List<Table1> resultList = query.getResultList();
Here's another way...
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
CriteriaQuery<Table1> criteriaQuery = criteriaBuilder.createQuery(Table1.class);
Root<Table1> table1 = criteriaQuery.from(Table1.class);
Expression<Byte> expression = table1.get("tmstmp"); // ...NOTE: column name is "tmstmp"...
Expression<Timestamp> castFunction = criteriaBuilder.function("TIMESTAMP", Timestamp.class, expression);
Predicate predicate = criteriaBuilder.greaterThanOrEqualTo(castFunction, tmstmp);
//NOTE: should be able to add to predicate...
// e.g.,
// predicate = criteriaBuilder.and(predicate, otherPredicate);
criteriaQuery.where(predicate);
TypedQuery<Table1> query = entityManager.createQuery(criteriaQuery);
List<Table1> resultList = query.getResultList();
答案 1 :(得分:-1)
这对你有用吗?
query.setParameter("tmstmp", tmstmp, TemporalType.DATE)
更新:
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "TMSTMP", nullable = false)
private Date tmstmp;