specification-arg-resolver
specification-arg-resolver copied to clipboard
Perform a typecast upon the expression.
Is it possible to perform a type cast upon the expression? For example, type cast LocalDateTime
to String
.
What I'm trying to achieve.
var localDate = "2018-01"
criteriaBuilder.like(root.get("localDateTimestamp").as(String.class), "%" +localDate + "%");
@Spec(typeCast=String.class)
Not sure if this relates to your case, but I use the Hibernate @Formula
on my entity to create a virtual column as cast to string and compare it at the database level:
@Formula("created_timestamp::varchar")
private String createdTimestampString;
and then use the @Spec
to match it:
@Spec(path="createdTimestampString", params="createdBefore", spec= LessThan.class)
which yields a query:
where calculatio0_.created_timestamp::varchar<=?
My sample uses LessThan, but same applies to the Like spec.
I think this really depends on your DB engine -- some kind of a function/conversion must be executed on the sql level, I think. The solution provided by @vanjad-sixt is another approach. I'm closing this topic for now, as it seems to be out of scope of the library. But feel free to reopen if you feel that more discussion is needed.