如何在Hibernate的本机SQL查询中转义冒号`:`?

我有一个本机SQL查询,如下所示:

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 ...");

问题是,Hibernate的解释:DOAATLTC\",:ltc\",:ltchnom\",作为参数,并希望我们给值query.setString("DOAATLTC\",",

...)query.setString("ltc\",", ...)query.setString("ltchnom\",", ...)

但是我不想让Hibernate这样解释,所以我想逃避冒号:

怎么做 ?

回答:

在Hibernate中,转义以开头\。但是在Java中,您还必须逃脱\另一个\。因此,所有内容都:需要替换为\\:。最后,您得到:

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 ...");

以上是 如何在Hibernate的本机SQL查询中转义冒号`:`? 的全部内容, 来源链接: utcz.com/qa/423996.html

回到顶部