具有大于或小于hibernate命名的sql查询

具有大于或小于hibernate命名的sql查询

本文介绍了具有大于或小于hibernate命名的sql查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



命名查询最初与用户输入的日期相匹配,并且它工作正常。
但是,当我将equals('=')更改为小于('< =')时,它给了我下面的错误。

 原因:org.hibernate.MappingException:无法解析输入流中的映射文档
在org.hibernate.cfg.Configuration.addInputStream(Configuration.java:431)
at org.hibernate.cfg.Configuration.addResource(Configuration.java:482)
... 106 more
引起:org.dom4j.DocumentException:文档第57行的错误:元素的内容必须由格式良好的字符数据或标记组成。嵌套异常:元素的内容必须包含格式正确的字符数据或标记。
at org.dom4j.io.SAXReader.read(SAXReader.java:482)
at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:422)
... 107更多

这是因为XML解析器不允许在内容中使用'&'或'>'的标签。但'<'或'>'对于形成< =或> =比较是必需的。是否有替代方式来表示大于或小于分析器的快乐程度。



注意:我们已经知道我们可以将指定查询作为注释添加到代码中,但我更喜欢用这种方式来保持系统一致性。



示例命名查询:

 < sql-query name =persons> 
< return alias =personclass =eg.Person/>
SELECT person.NAME AS {person.name},
person.AGE AS {person.age},
person.SEX AS {person.sex}
FROM PERSON人
WHERE person.NAME LIKE:namePattern
AND trim(person.JOINDATE)< = to_date(:joinDate,'dd-mm-yyyy')
< / sql-query>


解决方案

使用或& lt; < in xml。


I'm using hibernate 3 with named query in the hibernate configuration xml.

The named query originally matched a date with the user entered date, and it worked fine.But when I changed the equals ('=') to a less than ('<=') it gave me the following error.

Caused by: org.hibernate.MappingException: Could not parse mapping document in input stream
    at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:431)
    at org.hibernate.cfg.Configuration.addResource(Configuration.java:482)
    ... 106 more
Caused by: org.dom4j.DocumentException: Error on line 57 of document  : The content of elements must consist of well-formed character data or markup. Nested exception: The content of elements must consist of well-formed character data or markup.
    at org.dom4j.io.SAXReader.read(SAXReader.java:482)
    at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:422)
    ... 107 more

This is because the XML parser does not allow '<' or '>' inside the content of tags. But '<' or '>' is necessary to form the <= or >= comparison. Is there an alternate way to represent greater than or less than such that the parser is happy.

NB: I already know we can put the named query as annotation in the code, but I prefer it this way for system consistency.

Sample Named Query:

<sql-query name="persons">
    <return alias="person" class="eg.Person"/>
    SELECT person.NAME AS {person.name},
           person.AGE AS {person.age},
           person.SEX AS {person.sex}
    FROM PERSON person
    WHERE person.NAME LIKE :namePattern
    AND trim(person.JOINDATE) <= to_date(:joinDate, 'dd-mm-yyyy')
</sql-query>
解决方案

Use CDATA or &lt; to escape < in xml.

这篇关于具有大于或小于hibernate命名的sql查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 22:35