在将AND条件添加到下面提到的查询中
String querys = "SELECT dr.id,dr.creation_time,dr.drawing_spec_format,dr.end_time,dr.error_type,dr.last_access_time,dr.server_name,dr.start_time,dr.supply_unit,dr.client_id,ds.request_id,ds.bb,ds.car_offset_g,ds.car_sling_type,ds.car_type,ds.ch,ds.country"
+ " FROM FlcDrawingRequests dr, FlcDrawingRequestStats ds "
+ " where dr.id=ds.request_id"
+ " AND "
+ "(dr.start_time > "
+ monthStartDate
+ ")"
+ " AND "
+ "(dr.start_time <= "
+ monthEndDate + ")";
我收到以下异常
java.lang.IllegalArgumentException:
org.hibernate.hql.ast.QuerySyntaxException: unexpected token: 00 near line 1, column 851 [SELECT dr.id,dr.creation_time,dr.drawing_spec_format,dr.end_time,dr.error_type,dr.last_access_time,dr.server_name,dr.start_time,dr.supply_unit,dr.client_id,ds.request_id,ds.bb,ds.car_offset_g,ds.car_sling_type,ds.car_type,ds.ch,ds.country FROM com.kone.kss.cad.flcws.FlcDrawingRequests dr, com.kone.kss.cad.flcws.FlcDrawingRequestStats ds where dr.id=ds.request_id AND (dr.start_time > 2019-04-01 00:00:00) AND (dr.start_time <= 2019-4-2 23:59:59)]
at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:624)
at org.hibernate.ejb.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:96)
在这个问题上,你能帮我吗!
答案 0 :(得分:1)
此答案假定您正在尝试执行本机查询。无论如何,您都应该使用准备好的语句,这实际上将解决错误的根源,这与未加引号的日期文字有关。
String sql = "SELECT dr.id, dr.creation_time, dr.drawing_spec_format, dr.end_time, dr.error_type, dr.last_access_time, dr.server_name, dr.start_time, dr.supply_unit, dr.client_id, ds.request_id, ds.bb, ds.car_offset_g, ds.car_sling_type, ds.car_type, ds.ch, ds.country ";
sql += "FROM FlcDrawingRequests dr ";
sql += "INNER JOIN FlcDrawingRequestStats ds ";
sql += "ON dr.id = ds.request_id AND ";
sql += "dr.start_time > ?1 AND ";
sql += "dr.start_time <= ?2";
Query q = em.createNativeQuery(sql);
q.setParameter(1, monthStartDate);
q.setParameter(2, monthEndDate);
请注意,我还用显式内部联接替换了您的旧式隐式联接。这是在现代SQL中编写联接的首选方式。