I have entity Event with following fields:
@Temporal(TemporalType.TIMESTAMP)
@JsonFormat(pattern = DATE_FORMAT)
private Date start;
@Column(name = "period_minutes")
private Integer period;
I need to compare an amount of these fields with current date time:
SELECT * FROM Event e WHERE (e.start + e.period) < NOW();
Other words, I need add to start
field (which is date) some amount of minutes from period
(for example 15) and compare it with current datetime. I tried to do this like:
return (root, query, cb) -> {
Expression<Long> sum = cb.sum(root.<Date>get("start"), root.get("period"));
return cb.lessThan(sum, new Date());
};
But this does not compile. And I don't know how to convert the date to long and add period * 60 * 1000
(minutes -> ms). How to fix this issue?
SOLUTION
I have fixed this issue with help of @Серг0
cb.lessThan(
cb.sum(
cb.function("unix_timestamp", Long.class, root.get("start")),
cb.prod(root.get("period"), 1L)
),
cb.function("unix_timestamp", Long.class, cb.function("now", Long.class))
);
But I can't understand why if I use 1L
it works, and if I use 60L*1000L
(because I need to calculate milliseconds of period
that is in minutes) it does not work.