小编典典

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

hibernate

我有一个本机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这样解释,所以我想逃避冒号:

怎么做 ?


阅读 727

收藏
2020-06-20

共1个答案

小编典典

在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 ...");
2020-06-20