简体   繁体   中英

how to use setParameter for date field in where condition in sql query " date like :today " using hibernate

I am having trouble while trying to implement where condition with " punchDate like :date1 " in sql query using hibernate. Can any one please tell me what is the correct syntax to implement it.

String sql=select * from PunchHistory whered punchDate like :date1;
String date="2017-10-23";
List<PunchHistory> results =session.createQuery(StackperksConstants.sql)
                    .setDate("date1", java.sql.Date.valueOf(date))
                    .list();

Could anyone please help me. Thanks in advance

Like is used with Strings, instead you have to use = with dates :

String sql="select * from PunchHistory where punchDate = :date1"
//-----------------------------------------------------^
List<PunchHistory> results = 
                 session.createQuery(StackperksConstants.sql)
                 .setParameter("date1", java.sql.Date.valueOf(date), TemporalType.TIMESTAM)
//Instead you can use -----^
                 .list();

更好的方法是使用setParameter()方法,例如:

session.createQuery(sql,Class.class).setPrameter(0,date,TemporalType.TIMESTAMP).list();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM