Hibernate:命名查询中的错误,因为冒号“:”在评论中 [英] Hibernate: Error in named query because of colon ":" in comment

查看:664
本文介绍了Hibernate:命名查询中的错误,因为冒号“:”在评论中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个本机Sql namedQuery,它的insert语句如下所示:

  insert into myTable(
column1,
column2)
值(
'value1'
,'value2'/ * value2:非常重要* /

运行上面的块给出异常:命名查询中的错误,但是当我从注释中删除冒号如下:

 插入myTable(
column1,
column2)
值(
'value1'
,'value2'/ * value2非常重要* /

一切运行良好,任何解释?



编辑:
我可以在这里复制我的真实陈述,但它真的很长。以上只是样本。
下面是异常stacktrace:

 引起的:org.hibernate.HibernateException:命名查询中的错误:insertSalesDocument 
在org.hibernate.impl.SessionFactoryImpl。< init>(SessionFactoryImpl.java:435)
在org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1385)
在org .springframework.orm.hibernate3.LocalSessionFactoryBean.newSessionFactory(LocalSessionFactoryBean.java:860)
在org.springframework.orm.hibernate3.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:779)
在org.springframework.orm .html.bat .support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanF actory.java:1452)
... 194更多

其实我花了几个几个小时,知道为什么我收到这个异常,因为我的insert语句看起来正确,当我将其复制到Oracle Sql Developer时正常工作。它似乎是hibernate namedQuery解析器或某些东西的错误。

解决方案

Hibernate解析SQL并尝试在本机中查找任何参数查询。但解析器是有限的,不会将注释识别为可忽略的东西。



您可以将 code> \ :

 插入myTable(
column1,
column2)
值(
'value1'
,'value2'/ * value2\:非常重要* /


I'm writing a native Sql namedQuery, it's insert statement looks like the following:

insert into myTable (
   column1,
   column2)
values(
   'value1'
   ,'value2' /* value2: is very important */
)

running the above block gives exception: "Error in named query", but when I remove the colon from the comment as following:

 insert into myTable (
    column1,
    column2)
 values(
    'value1'
    ,'value2' /* value2 is very important */
    )

everything runs fine, any explanation?

EDIT: I can copy my real statement here but it's really long one. The above is just sample. Here is the exception stacktrace:

Caused by: org.hibernate.HibernateException: Errors in named queries: insertSalesDocument
    at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:435)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1385)
    at org.springframework.orm.hibernate3.LocalSessionFactoryBean.newSessionFactory(LocalSessionFactoryBean.java:860)
    at org.springframework.orm.hibernate3.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:779)
    at org.springframework.orm.hibernate3.AbstractSessionFactoryBean.afterPropertiesSet(AbstractSessionFactoryBean.java:211)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1514)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1452)
    ... 194 more

Actually it took me couple of hours to know why I'm getting this exception since my insert statement looks correct and works properly when I copy it to Oracle Sql Developer. It looks to be a bug with hibernate namedQuery parser or something.

解决方案

Hibernate parses the SQL and tries to find any parameter in native queries. But the parser is limited and will not recognize the comment as something ignorable.

You can escape the : with \:

insert into myTable (
   column1,
   column2)
values(
   'value1'
   ,'value2' /* value2\: is very important */
)

这篇关于Hibernate:命名查询中的错误,因为冒号“:”在评论中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆