2014-05-17 164 views
0

我使用Spring MVC 3和Hibernate 4休眠4 + org.hibernate.MappingException:未知實體

下面是代碼來執行DAO操作

@Repository("headCategoryDao") 
@Transactional 
public class HeadCategoryDaoImpl extends CustomHibernateDaoSupport<HeadCategory, Integer> implements HeadCategoryDao { 

    @Override 
    public void delete(HeadCategory headCategory) throws InstanceNotFoundException { 
     getHibernateTemplate().delete(headCategory); 
    } 

    @Override 
    public HeadCategory findHeadCategoryByName(String headCategoryName) throws InstanceNotFoundException { 
     List list = getHibernateTemplate().createCriteria("from HeadCategory where headCategoryName="+headCategoryName).list(); 
     if(list.isEmpty()) { 
      throw new InstanceNotFoundException("HeadCategory not found for provided headCategoryName"); 
     } 
     return (HeadCategory) list.get(0); 
    } 

    @Override 
    public List<HeadCategory> getHeadCategoriesByMainType(WealthConstants.ACCOUNT_TYPE mainCategoryType) { 
     List list = getHibernateTemplate().createCriteria("from HeadCategory where mainCategoryType="+ mainCategoryType).list();   
     return list; 
    } 
} 

findHeadCategoryByName方法稱爲拋出異常的下方,什麼可以是問題?

nested exception is org.hibernate.MappingException: Unknown entity: from HeadCategory where headCategoryName=Cash In Hand] with root cause 
org.hibernate.MappingException: Unknown entity: from HeadCategory where headCategoryName=Cash In Hand 
    at org.hibernate.internal.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:1131) 
    at org.hibernate.internal.SessionImpl.getOuterJoinLoadable(SessionImpl.java:1737) 
    at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1642) 
    at org.hibernate.internal.CriteriaImpl.list(CriteriaImpl.java:374) 
    at com.wms.dao.impl.common.HeadCategoryDaoImpl.findHeadCategoryByName(HeadCategoryDaoImpl.java:24) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:606) 
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317) 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183) 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150) 
    at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:96) 
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:260) 
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:94) 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) 
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204) 
    at com.sun.proxy.$Proxy35.findHeadCategoryByName(Unknown Source) 
    at com.wms.service.impl.common.HeadCategoryServiceImpl.findHeadCategoryByName(HeadCategoryServiceImpl.java:48) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:606) 
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317) 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183) 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150) 
    at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:96) 
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:260) 
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:94) 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) 
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204) 
    at com.sun.proxy.$Proxy36.findHeadCategoryByName(Unknown Source) 
    at com.wms.web.helper.ScriptHelper.headsScript(ScriptHelper.java:282) 
    at com.wms.web.helper.ScriptHelper.initialDataScriptExecution(ScriptHelper.java:130) 
    at com.wms.web.controller.StaticController.buildDataUsingScript(StaticController.java:25) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:606) 
    at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:219) 
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132) 
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104) 
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:745) 
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:686) 
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80) 
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925) 
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856) 
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:936) 
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:827) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:621) 

回答

0

查看查詢:

from HeadCategory where headCategoryName=Cash In Hand 

這不是一個有效的查詢都沒有。什麼是現金?什麼是手?正確的查詢是

from HeadCategory where headCategoryName='Cash In Hand' 

瞭解正確,安全地傳遞參數查詢:

session.createQuery("from HeadCategory where headCategoryName = :name") 
     .setString("name", headCategoryName) 
     .list(); 
相關問題