Ketan Bhavsar
Ketan Bhavsar

Reputation: 5396

Hibernate session.createQuery error while replace method with single quote

I got very typical issue. My dynamically generated query like this...

UPDATE Templates t SET t.TEMPLATE_DATA =  replace(t.TEMPLATE_DATA, 'Test\'s Test', 'Kent"s Test'), t.TEMPLATE_DATA =  replace(t.TEMPLATE_DATA, 'Test"s Test', 'Kent"s Test'), UPDATE_DATE = NOW() where PRACTICE_ID = 1 AND CATEGORY_ID IN (1)

This works perfect when I explictily fire this query in db. but by using hibernate's session.createQuery(-- my query --) if thwows an error QueryTranslatorException.

Database : Mysql 5.3

Have any one faced this issue? Thanks in advance.

Upvotes: 2

Views: 1997

Answers (2)

dimas
dimas

Reputation: 6073

Try to run this in Hibernate as native SQL query:

session.createSQLQuery(-- query text --);

Because if you use

session.createQuery(-- query text --);

Hibernate will try to execute it as HQL query which differs from usual SQL query.

HQL is object oriented query language. It operates in terms of objects rather then in terms of tables. Here posted a brief description of difference between SQL and HQL. But if you have time better to read appropriate sections of hibernate's documentation about HQL and Native SQL usage.

Upvotes: 3

Nandkumar Tekale
Nandkumar Tekale

Reputation: 16158

If you want to execute SQL Query in hibernate, Use : session.createSQLQuery(String query);

Upvotes: 2

Related Questions