I have a native SQL query that looks like that :
Query query = session.createSQLQuery("SELECT
XMLSERIALIZE
(CONTENT
XMLELEMENT
(
NAME \"ltc:DOAATLTC\",
XMLATTRIBUTES
(
'http://www.edftrading.com/Trade/Common/DoaatLTC' AS \"xmlns:ltc\",
'http://www.edftrading.com/Trade/Common/DoaatLTCHourlyNomination' AS \"xmlns:ltchnom\"
),
XMLELEMENT ( ... ) FROM ...");
The thing is that Hibernate interprets :DOAATLTC\",
, :ltc\",
, :ltchnom\",
as parameters and expects that we give values query.setString("DOAATLTC\",", ...)
, query.setString("ltc\",", ...)
, query.setString("ltchnom\",", ...)
But I do not want Hibernate to interpret like that, so I want to escape the colon :
.
How to do that ?
If your colon is a cast like SELECT reltuples::BIGINT then you can rewrite it as a cast(reltuples as BIGINT) to avoid the colons.
For Hibernate Native SQL Query, we use Session. createSQLQuery(String query) to create the SQLQuery object and execute it. For example, if you want to read all the records from Employee table, we can do it through below code. When we execute above code for the data setup we have, it produces following output.
escape Keyword execute(sql); If you use the backslash character (\) as the escape character, you also have to use two backslash characters in your Java String literal, because the backslash is also a Java escape character.
You can use native SQL to express database queries if you want to utilize database-specific features such as query hints or the CONNECT keyword in Oracle. Hibernate 3. x allows you to specify handwritten SQL, including stored procedures, for all create, update, delete, and load operations.
If your colon is a cast like SELECT reltuples::BIGINT
then you can rewrite it as a cast(reltuples as BIGINT)
to avoid the colons.
ref
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With