How to escape colon `:` within a native SQL query in Hibernate?

前端 未结 2 1870
再見小時候
再見小時候 2021-01-12 02:12

I have a native SQL query that looks like that :

Query query = session.createSQLQuery(\"SELECT
        XMLSERIALIZE
        (CONTENT
          XMLELEMENT
           


        
相关标签:
2条回答
  • 2021-01-12 02:23

    In Hibernate, escaping is done with prepending by \. But in Java, you also have to escape \ by another \. So every : needs to be replaced by \\:. Finally, you get:

    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 ...");
    
    0 讨论(0)
  • 2021-01-12 02:41

    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

    0 讨论(0)
提交回复
热议问题