CriteriaBuilder Cast Date to Long


I have the following table:

CREATE TABLE "pre_vendas" (
	"id_prev_venda" BIGINT NOT NULL,
	"data_prev_venda" DATE NOT NULL,
	"data_real_venda" DATE NOT NULL,	
	"valor_real_venda" NUMERIC(10,2) NULL DEFAULT NULL,	
	PRIMARY KEY ("id_prev_venda")

I’m having difficulty converting the dates in numeric format (numbers only), I’m trying this way:

CriteriaBuilder builder = sf.getCurrentSession().getCriteriaBuilder();
CriteriaQuery<Tuple> criteria = builder.createTupleQuery();
Root<PreVenda> root = criteria.from(PreVenda.class);

Expression<Long> dataPrevVenda = root.get(PreVenda_.dataPrevVenda).as(Long.class);

Expression<Long> dataRealVenda = root.get(PreVenda_.dataRealVenda).as(Long.class);,dataPrevVenda)));

this is returning a cast exception, Anyone have any idea how to solve this?

First, you have to use an SQL function to get the number representation of a DATE.

Second, you could also use the DATEDIFF function if the underlying DB supports it.

Hello, Thanks for the reply,

Maybe I would have a way to do a cast, because I wanted to make one date less than another, to do this the two need to be numerical, if I use the DATEDIFF function it will work for example for database Mysql however in other databases like Postgres will not work .

You can use the CAST SQL function for that.

I don’t think that is translated to an SQL CAST automatically.