如何使用hibernate,HSQL按日期查询过滤器

时间:2021-10-08 12:59:55

Good afternoon , I need to filter a query by date, the date is defined in the database as: y: m: d. It 's performed with sqlnativo in hibernate but it returns null. anyone can help me , also if someone can explain me how to handle criteria with dates, since I do not find any clear example

下午好,我需要按日期过滤查询,日期在数据库中定义为:y:m:d。它是在hibernate中使用sqlnativo执行的,但它返回null。任何人都可以帮助我,如果有人可以解释我如何处理日期标准,因为我没有找到任何明确的例子

    Query query = session.createSQLQuery("Select  id,fecha,total,idEnvio FROM VENTA_CABECERA vc WHERE date(fecha) = :fec" ).addEntity(HeadSale.class);
query.setParameter("fec", date);
return query.list();

It can also be the between clause , but honestly I can not make it work. would be grateful to give me a solution. regards

它也可以是之间的条款,但说实话,我不能使它工作。不胜感激,给我一个解决方案。问候

1 个解决方案

#1


1  

change the format of "fec" parameter as y: m: d and remove date function inside query

将“fec”参数的格式更改为y:m:d并删除查询中的日期函数

DateFormat df;    
df= new SimpleDateFormat("y: m: d");
String dt= sdf.format(date);    
Query query = session.createSQLQuery
("Select  id,fecha,total,idEnvio FROM VENTA_CABECERA vc WHERE fecha = :fec" )
.addEntity(HeadSale.class);
query.setParameter("fec", dt);
return query.list();

#1


1  

change the format of "fec" parameter as y: m: d and remove date function inside query

将“fec”参数的格式更改为y:m:d并删除查询中的日期函数

DateFormat df;    
df= new SimpleDateFormat("y: m: d");
String dt= sdf.format(date);    
Query query = session.createSQLQuery
("Select  id,fecha,total,idEnvio FROM VENTA_CABECERA vc WHERE fecha = :fec" )
.addEntity(HeadSale.class);
query.setParameter("fec", dt);
return query.list();