休眠:- 未找到本机查询异常

Posted

技术标签:

【中文标题】休眠:- 未找到本机查询异常【英文标题】:Hibernate :- Native Query not found Exception 【发布时间】:2013-10-05 17:36:17 【问题描述】:

我在 Spring3+JSF2 的项目中使用 Hibernate4 我在 DB 表上定义了一个 Java 类的本地查询,但是当我运行项目时出现异常。

SEVERE: org.springframework.orm.hibernate3.HibernateSystemException: Named query not known: findAllloceValue; nested exception is org.hibernate.MappingException: Named query not known: findAllloceValue
    at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:690)
    at org.springframework.orm.hibernate3.HibernateAccessor.convertHibernateAccessException(HibernateAccessor.java:412)
    at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:411)
    at org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374)
    at org.springframework.orm.hibernate3.HibernateTemplate.findByNamedQuery(HibernateTemplate.java:979)
    at org.springframework.orm.hibernate3.HibernateTemplate.findByNamedQuery(HibernateTemplate.java:971)
    at com.kuber.data.db.WCompanyDataDao.fetchAnnouncementType(WCompanyDataDao.java:88)
    at com.kuber.service.dao.CompanyDataService.getAnnouncementType(CompanyDataService.java:65)
    at com.kuber.web.view.AnnouncementBean.fetchAnnouncementType(AnnouncementBean.java:75)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.apache.el.parser.AstValue.invoke(AstValue.java:278)
    at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:274)
    at javax.faces.event.MethodExpressionActionListener.processAction(MethodExpressionActionListener.java:153)
    at javax.faces.event.ActionEvent.processListener(ActionEvent.java:88)
    at javax.faces.component.UIComponentBase.broadcast(UIComponentBase.java:769)
    at javax.faces.component.UICommand.broadcast(UICommand.java:300)
    at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:795)
    at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1260)
    at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.primefaces.webapp.filter.FileUploadFilter.doFilter(FileUploadFilter.java:79)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at com.ocpsoft.pretty.PrettyFilter.doFilter(PrettyFilter.java:145)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at com.kuber.web.server.KuberFilter.doFilter(KuberFilter.java:54)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:611)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: org.hibernate.MappingException: Named query not known: findAllloceValue
    at org.hibernate.impl.AbstractSessionImpl.getNamedQuery(AbstractSessionImpl.java:93)
    at org.hibernate.impl.SessionImpl.getNamedQuery(SessionImpl.java:1407)
    at org.springframework.orm.hibernate3.HibernateTemplate$33.doInHibernate(HibernateTemplate.java:981)
    at org.springframework.orm.hibernate3.HibernateTemplate$33.doInHibernate(HibernateTemplate.java:979)
    at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:406)
    ... 47 more

Oct 5, 2013 10:52:41 PM com.sun.faces.context.AjaxExceptionHandlerImpl log
SEVERE: JSF1073: javax.faces.event.AbortProcessingException caught during processing of INVOKE_APPLICATION 5 : UIComponent-ClientId=searchBtn, Message=org.springframework.orm.hibernate3.HibernateSystemException: Named query not known: findAllloceValue; nested exception is org.hibernate.MappingException: Named query not known: findAllloceValue
Oct 5, 2013 10:52:41 PM com.sun.faces.context.AjaxExceptionHandlerImpl log
SEVERE: org.springframework.orm.hibernate3.HibernateSystemException: Named query not known: findAllloceValue; nested exception is org.hibernate.MappingException: Named query not known: findAllloceValue
javax.faces.event.AbortProcessingException: org.springframework.orm.hibernate3.HibernateSystemException: Named query not known: findAllloceValue; nested exception is org.hibernate.MappingException: Named query not known: findAllloceValue
    at javax.faces.event.MethodExpressionActionListener.processAction(MethodExpressionActionListener.java:182)
    at javax.faces.event.ActionEvent.processListener(ActionEvent.java:88)
    at javax.faces.component.UIComponentBase.broadcast(UIComponentBase.java:769)
    at javax.faces.component.UICommand.broadcast(UICommand.java:300)
    at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:795)
    at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1260)
    at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.primefaces.webapp.filter.FileUploadFilter.doFilter(FileUploadFilter.java:79)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at com.ocpsoft.pretty.PrettyFilter.doFilter(PrettyFilter.java:145)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at com.kuber.web.server.KuberFilter.doFilter(KuberFilter.java:54)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:611)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: org.springframework.orm.hibernate3.HibernateSystemException: Named query not known: findAllloceValue; nested exception is org.hibernate.MappingException: Named query not known: findAllloceValue
    at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:690)
    at org.springframework.orm.hibernate3.HibernateAccessor.convertHibernateAccessException(HibernateAccessor.java:412)
    at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:411)
    at org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374)
    at org.springframework.orm.hibernate3.HibernateTemplate.findByNamedQuery(HibernateTemplate.java:979)
    at org.springframework.orm.hibernate3.HibernateTemplate.findByNamedQuery(HibernateTemplate.java:971)
    at com.kuber.data.db.WCompanyDataDao.fetchAnnouncementType(WCompanyDataDao.java:88)
    at com.kuber.service.dao.CompanyDataService.getAnnouncementType(CompanyDataService.java:65)
    at com.kuber.web.view.AnnouncementBean.fetchAnnouncementType(AnnouncementBean.java:75)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.apache.el.parser.AstValue.invoke(AstValue.java:278)
    at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:274)
    at javax.faces.event.MethodExpressionActionListener.processAction(MethodExpressionActionListener.java:153)
    ... 34 more
Caused by: org.hibernate.MappingException: Named query not known: findAllloceValue
    at org.hibernate.impl.AbstractSessionImpl.getNamedQuery(AbstractSessionImpl.java:93)
    at org.hibernate.impl.SessionImpl.getNamedQuery(SessionImpl.java:1407)
    at org.springframework.orm.hibernate3.HibernateTemplate$33.doInHibernate(HibernateTemplate.java:981)
    at org.springframework.orm.hibernate3.HibernateTemplate$33.doInHibernate(HibernateTemplate.java:979)
    at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:406)
    ... 47 more

谁能告诉我什么时候做错了我的 Tables java 类是

package com.kuber.data.orm.user;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.ColumnResult;
import javax.persistence.Entity;
import javax.persistence.SqlResultSetMapping;
import javax.persistence.SqlResultSetMappings;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.hibernate.annotations.NamedNativeQueries;
import org.hibernate.annotations.NamedNativeQuery;

@NamedNativeQueries(
      @NamedNativeQuery(
              name = "findAllloceValue",
              query = "select distinct as loce_value from lo_config_expression where loce_context='Announcement'",          
              resultSetMapping=  "netConfigMapping") ,
    )

    @SqlResultSetMappings( 
        @SqlResultSetMapping(
                name="netConfigMapping", columns =  
                @ColumnResult(name = "loce_value"),
            )      
    )

@Entity
@Table(name = "lo_config_expression")
public class LoConfigExpression implements java.io.Serializable 

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
     private String loceContext;
     private String loceExpression;
     private String loceValue;
     private Date createdTm;
     private Date lastModTm;
    public  LoConfigExpression()
    

    

    public  LoConfigExpression(String loceContext,String loceExpression,String loceValue,Date createdTm, Date lastModTm)
    
        this.loceContext=loceContext;
        this.loceExpression=loceExpression;
        this.loceValue=loceValue;
        this.createdTm=createdTm;
        this.lastModTm=lastModTm;
    
    @Column(name = "loce_context")
    public String getLoceContext() 
        return loceContext;
    

    public void setLoceContext(String loceContext) 
        this.loceContext = loceContext;
    
    @Column(name = "loce_value", length = 256)
    public String getLoceValue() 
        return loceValue;
    

    public void setLoceValue(String loceValue) 
        this.loceValue = loceValue;
    
    @Column(name = "loce_expression", length = 256)
    public String getLoceExpression() 
        return loceExpression;
    

    public void setLoceExpression(String loceExpression) 
        this.loceExpression = loceExpression;
    
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "created_tm")
    public Date getCreatedTm() 
        return createdTm;
    

    public void setCreatedTm(Date createdTm) 
        this.createdTm = createdTm;
    

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "last_mod_tm")
    public Date getLastModTm() 
        return lastModTm;
    

    public void setLastModTm(Date lastModTm) 
        this.lastModTm = lastModTm;
    

【问题讨论】:

你能分享你的表格脚本可能是你的课错了 【参考方案1】:

你应该导入这些,用于原生查询;

import javax.persistence.NamedNativeQueries;
import javax.persistence.NamedNativeQuery;

【讨论】:

【参考方案2】:

您不应该在本机查询中使用普通 SQL 吗?

您尝试执行的查询不是 SQL,而是 HQL。尝试将其重写为普通 SQL。

【讨论】:

但其简单的 SQL 查询 select distinct as loce_value from lo_config_expression where loce_context='Announcement' 我在查询中使用的是表名和列名,它不是 HSQL 我没有得到你的线索,你能详细说明一下吗? 我删除了与查询不同但相同的异常 删除 distinct 无济于事。您在选择中缺少字段名称

以上是关于休眠:- 未找到本机查询异常的主要内容,如果未能解决你的问题,请参考以下文章

使用休眠的本机查询

返回休眠本机 sql 查询

如何使用休眠本机查询获取单个记录字符串值?

休眠 Java:未找到函数“AES_DECRYPT”; SQL 语句:

休眠异常:意外令牌:HAVING

Spring - 当列名与模型中的名称不同时,本机查询与休眠混合