在 JPA Criteria Builder 中按 Oracle 函数排序

Order By Oracle Function In JPA Criteria Builder

我有以下 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);