Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
158 views
in Technique[技术] by (71.8m points)

java - Hibernate: Error in named query because of colon ":" in comment

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 */
)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...