我正在尝试创建一个涉及 SQL Server datetime2 字段的 where 子句(精确到 100 纳秒);使用 JPA 和休眠。
我的代码看起来像这样:
CriteriaBuilder cb = em.getCriteriaBuilder();
List<Predicate> predicates = new ArrayList<>();
CriteriaQuery(X) q = cb.createQuery(X.class);
Root<X> root = q.from(X.class);
java.sql.Timestamp mySqlTimeStamp = Timestamp.valueOf("2015-06-04 11:31:53.2119339");
predicates.add(cb.greaterThan(root.get("DateModified"), mySqlTimeStamp))
q.where(predicates.toArray(new Predicate[predicates.size()]));
em.createQuery(q).getResultList();
SQL Server Profiler 显示时间戳参数值被截断为2015-06-04 11:31:53.210
- 奇怪,这甚至不是四舍五入。不用说,我的结果集不准确。
如果我手动将参数更改为完整值,2015-06-04 11:31:53.2119339
一切都很好。
问题是,如何让 JPA不截断日期?
或者,如何为我的时间戳字段注入我自己的参数值序列化程序?
帮助表示赞赏;谢谢
更新
我已经将它跟踪到这个 jtds jdbc 代码
net.sourceforge.jtds.jdbc.JtdsPerparedStatement
::
protected void setParameter(int parameterIndex, Object x, int targetSqlType, int scale, int length)
throws SQLException {
ParamInfo pi = getParameter(parameterIndex);
if ("ERROR".equals(Support.getJdbcTypeName(targetSqlType))) {
throw new SQLException(Messages.get("error.generic.badtype",
Integer.toString(targetSqlType)), "HY092");
}
// Update parameter descriptor
if (targetSqlType == java.sql.Types.DECIMAL
|| targetSqlType == java.sql.Types.NUMERIC) {
pi.precision = connection.getMaxPrecision();
if (x instanceof BigDecimal) {
x = Support.normalizeBigDecimal((BigDecimal) x, pi.precision);
pi.scale = ((BigDecimal) x).scale();
} else {
pi.scale = (scale < 0) ? TdsData.DEFAULT_SCALE : scale;
}
} else {
pi.scale = (scale < 0) ? 0 : scale;
}
if (x instanceof String) {
pi.length = ((String) x).length();
} else if (x instanceof byte[]) {
pi.length = ((byte[]) x).length;
} else {
pi.length = length;
}
if (x instanceof Date) {
x = new DateTime((Date) x);
} else if (x instanceof Time) {
x = new DateTime((Time) x);
} else if (x instanceof Timestamp) {
x = new DateTime((Timestamp) x);
}
pi.value = x;
pi.jdbcType = targetSqlType;
pi.isSet = true;
pi.isUnicode = connection.getUseUnicode();
}
}
强制net.sourceforge.jtds.jdbc.DateTime
输入时间戳的位置,仅支持毫秒。