我有以下SQL
SELECT ID,LASTTIMEEXECUTEDDATE as d FROM STATISTICSDATE ORDER BY LASTTIMEEXECUTEDDATE
使用CriteriaBuilder可以正常工作:
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<StatisticsDate> cq = cb.createQuery(StatisticsDate.class);
Root<StatisticsDate> rootEntry = cq.from(StatisticsDate.class);
CriteriaQuery<StatisticsDate> all = cq.select(rootEntry).orderBy(cb.desc(
rootEntry.get("lastTimeExecutedDate")));
TypedQuery<StatisticsDate> allQuery = em.createQuery(all);
但是现在我需要使用以下方法获得更准确的结果:
SELECT ID,LASTTIMEEXECUTEDDATE as d FROM STATISTICSDATE ORDER BY
to_timestamp(LASTTIMEEXECUTEDDATE, 'DD.MM.YYYY:HH24:MI:SS') desc;
我可以通过本机sql做到这一点,但我想知道是否可以通过CriteriaBuilder使用它。
麻烦我的是
to_timestamp(LASTTIMEEXECUTEDDATE, 'DD.MM.YYYY:HH24:MI:SS')
谢谢
最佳答案
像这样尝试:
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<StatisticsDate> cq = cb.createQuery(StatisticsDate.class);
Root<StatisticsDate> rootEntry = cq.from(StatisticsDate.class);
CriteriaQuery<StatisticsDate> all = cq.select(rootEntry).orderBy(cb.desc(
cb.function(
"to_timestamp", Timestamp.class,
rootEntry.get("lastTimeExecutedDate"),
cb.literal("DD.MM.YYYY:HH24:MI:SS")
)
));
TypedQuery<StatisticsDate> allQuery = em.createQuery(all);