Home > OS >  Cast LocaDateTime to Date in QueryDSL
Cast LocaDateTime to Date in QueryDSL

Time:01-26

Query DSL has castToNum() but is there anything to convert LocalDateTime(job_date_time) to Date ?

SELECT user_logon as logon, cast(job_date_time as date) as 
dateTime,SUM(total_pages) as total
from job_history group by user_logon, cast(job_date_time as date) order by 
dateTime desc

Java Approach

    JPAQuery<JobHistory> queryBuilder = new JPAQuery<>(entityManager);
    QJobHistory jobHistory = QJobHistory.jobHistory;
    queryBuilder.select(jobHistory.userLogon.as("logon"),jobHistory.jobDateTime.as("dateTime"),jobHistory.totalPages.sum().as("total"))
            .from(jobHistory)
            .groupBy(jobHistory.userLogon)
            .groupBy(jobHistory.jobDateTime)
            .orderBy(Expressions.stringPath("totalPages").desc());

            queryBuilder.fetchAll();

How can I cast this expression to date?

jobHistory.jobDateTime.as("dateTime")

And this expression to date?

.groupBy(jobHistory.jobDateTime)

What I tried

Expressions.asDate(jobHistory.jobDateTime).as("dateTime");

CodePudding user response:

Worked for me .

Expressions
.dateTimeTemplate(java.sql.Date.class,"cast(job_date_time as date)")
.as("dateTime");
  • Related