简体   繁体   中英

Spring Criteria query for math operation

I have a query which looks like below. We have module where we take requests from users and store it in report_request table with date when we received it. We keep that request in table for 30 days before removing it. We have scheduler which run every hour to check is there any request that is expiring today, if so delete that record from table.

Basically I am trying to get no of days between two timestamp (current_timestamp & report request timestamp). If no of days are 30 or more than I have to delete that record.

select round((1510184973334-1484905416098)/(24 * 60 * 60 * 1000));

I am trying to convert same into Predicate, so I can use Spring Repository to execute this query.

criteriaBuilder.greaterThan(criteriaBuilder.quot(criteriaBuilder.diff(System.currentTimeMillis(), join.get(Table_.endTime)), new Long(86400000)),
                                    new Long(1))

Here criteriaBuilder.quot returns Expression which is not acceptable in criteriaBuilder.greaterThan method.

Request everyone to review this code snippet and share your inputs. Please let me know if you require any other information on this. Or Also let me know if there is a better way to achieve this use case.

Regards.

You can run native SQL in the case or use Hibernate @Formula (see the example ) to introduce the field in your entity. Then the field could be used in your expression.

@Formula(value = “round((now()-end_time)/(24 * 60 * 60 * 1000))”)
private int days;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM