Escape quote in HQL query

佐手、 提交于 2019-12-13 17:17:34

问题


I'm developping an application with spring mvc and hibernate, i have a problem in my hql query when i want to select an object from the database by his name.

Because the name contain a quote (ingénieur d'étude)

String hql = "From Intervenant I Where I.profil like '"+profil+"'";

i had this exception :

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.hibernate.QueryException: expecting ''', found '<EOF>' [From com.model.Intervenant I Where I.profil like 'Ingénieur d'étude']

Is the user who inserts datas, so how I can know what the user inserted and where it has put the quote or any other special caracters ?

Please can you help me !


回答1:


Put single quote to escaping charater in mysql like following

profile = ingénieur d''étude;

OR

profile = ingénieur d\'étude;


来源:https://stackoverflow.com/questions/24607972/escape-quote-in-hql-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!