specification-arg-resolver icon indicating copy to clipboard operation
specification-arg-resolver copied to clipboard

Perform a typecast upon the expression.

Open NicklasWallgren opened this issue 5 years ago • 1 comments

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)

NicklasWallgren avatar Mar 22 '19 11:03 NicklasWallgren

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.

vanjad-sixt avatar Mar 13 '20 19:03 vanjad-sixt

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.

tkaczmarzyk avatar Dec 29 '22 15:12 tkaczmarzyk