HibernateException:命名查询中的错误:FindPostWithComments

HibernateException: Errors in named queries: FindPostWithComments

在我的 Spring 4 应用程序中,我遇到了命名本机查询的问题:

@NamedNativeQuery(name = "FindPostWithComments", query = 

    " SELECT * FROM ("
+   "       SELECT p.*, "
+   "       MATCH (description) AGAINST ('text') AS score " 
+   "       FROM posts as p"
+   "       WHERE MATCH (description) AGAINST ('text') > 0 "
+   "       LIMIT 0, 10 "
+   "   ) p LEFT JOIN ( "
+   "       SELECT c.*, " 
+   "           @rownumber := CASE WHEN @post_id = post_id THEN @rownumber + 1 ELSE 1 END AS n, "
+   "           @post_id := post_id "
+   "       FROM comments c, "
+   "       (SELECT @rownumber := 0, @post_id := 0) r " 
+   "       WHERE MATCH (content) AGAINST ('text') > 0 "
+   "       ORDER BY c.last_edited DESC, post_id DESC "
+   "   ) c ON p.post_id = c.post_id "
+   " WHERE c.post_id IS NULL OR n BETWEEN 1 and 3 "
+   "   ORDER BY score DESC "

, resultSetMapping = "PostWithComments")

@SqlResultSetMappings({ 
    @SqlResultSetMapping(
        name = "PostWithComments", 
        entities = { 
            @EntityResult(entityClass = Post.class, fields = {
                    @FieldResult(name = "id", column = "p.post_id"),
                    @FieldResult(name = "userId", column = "p.user_id"),
                    @FieldResult(name = "type", column = "p.type_id"),
                    @FieldResult(name = "description", column = "p.description"),
                    @FieldResult(name = "link", column = "p.link"),
                    @FieldResult(name = "dateCreated", column = "p.date_created"),
                    @FieldResult(name = "lastEdited", column = "p.last_edited"),
                    @FieldResult(name = "totalVotes", column = "p.total_votes"),
                    @FieldResult(name = "totalComments", column = "p.total_comments")                
                }
            ),
            @EntityResult(entityClass = Comment.class, fields = {
                    @FieldResult(name = "id", column = "c.comment_id"),
                    @FieldResult(name = "post", column = "c.post_id"),
                    @FieldResult(name = "userId", column = "c.user_id"),
                    @FieldResult(name = "dateCreated", column = "c.date_created"),
                    @FieldResult(name = "lastEdited", column = "c.last_edited"),
                    @FieldResult(name = "content", column = "c.content"),
                    @FieldResult(name = "totalVotes", column = "c.total_votes")
                }               
            )           
        }
    )
})

在应用程序启动期间失败,出现以下异常:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Invocation of init method failed; nested exception is org.hibernate.HibernateException: Errors in named queries: FindPostWithComments
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1566)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getObject(AbstractBeanFactory.java:303)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:956)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:747)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:403)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4728)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5162)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1409)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1399)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
Caused by: org.hibernate.HibernateException: Errors in named queries: FindPostWithComments
    at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:545)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1859)
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.perform(EntityManagerFactoryBuilderImpl.java:852)
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.perform(EntityManagerFactoryBuilderImpl.java:845)
    at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.withTccl(ClassLoaderServiceImpl.java:398)
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:844)
    at org.hibernate.jpa.HibernatePersistenceProvider.createContainerEntityManagerFactory(HibernatePersistenceProvider.java:152)
    at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:338)
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:318)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1625)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1562)
    ... 21 more

可能的原因是什么?

此查询完全适用于 MySQL workbench。

我自己找到了原因。我需要在每个冒号前添加双斜杠,所以最终查询必须如下所示:

    " SELECT * FROM ("
+   "       SELECT p.*, "
+   "       MATCH (description) AGAINST (:term) AS score " 
+   "       FROM posts as p"
+   "       WHERE MATCH (description) AGAINST (:term) > 0 "
+   "       LIMIT 0, 10 "
+   "   ) p LEFT JOIN ( "
+   "       SELECT c.*, " 
+   "           @rownumber \:= CASE WHEN @post_id = post_id THEN @rownumber + 1 ELSE 1 END AS n, "
+   "           @post_id \:= post_id "
+   "       FROM comments c, "
+   "       (SELECT @rownumber \:= 0, @post_id \:= 0) r " 
+   "       WHERE MATCH (content) AGAINST (:term) > 0 "
+   "       ORDER BY c.last_edited DESC, post_id DESC "
+   "   ) c ON p.post_id = c.post_id "
+   " WHERE c.post_id IS NULL OR n BETWEEN 1 and 3 "
+   "   ORDER BY score DESC "