org.hibernate.hql.ast.QuerySyntaxException:Employee_detail未映射[来自Employee_detail]

时间:2015-05-15 14:45:55

标签: java spring hibernate spring-mvc servlets

我的SQL查询是select * from employee_detail;

错误是: -

SEVERE: Servlet.service() for servlet [spring] in context with path [/Roster_APP] threw exception [Request processing failed; nested exception is org.hibernate.hql.ast.QuerySyntaxException: Employee_detail is not mapped [from Employee_detail]] with root cause
org.hibernate.hql.ast.QuerySyntaxException: Employee_detail is not mapped [from Employee_detail]
at org.hibernate.hql.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:158)
at org.hibernate.hql.ast.tree.FromElementFactory.addFromElement(FromElementFactory.java:87)
at org.hibernate.hql.ast.tree.FromClause.addFromElement(FromClause.java:70)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3056)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:2945)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:688)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:281)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:229)
at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:228)
at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:160)
at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:111)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:77)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:56)
at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:72)
at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:133)
at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:112)
at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1623)
at com.lak.dao.impl.EmployeeDaoImpl.getAllEmployee(EmployeeDaoImpl.java:45)
at com.lak.service.impl.EmployeeServiceImpl.getAllEmployee(EmployeeServiceImpl.java:44)
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.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:98)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:262)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:95)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
at com.sun.proxy.$Proxy24.getAllEmployee(Unknown Source)
at com.lak.controller.EmployeeContoller.getAllEmployee(EmployeeContoller.java:44)
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.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:215)
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:749)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:689)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:938)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:870)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:852)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:620)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:504)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:421)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1074)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:314)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)

控制器是:

@Controller
public class EmployeeContoller {
@Autowired
private EmployeeServices employeeServices;

@RequestMapping("/index")
public String setupForm(Map<String,Object>map){

    Employee employee=new Employee();
    map.put("employee", employee);
    map.put("employee_List", employeeServices.getAllEmployee());
    return "employee";

}



@RequestMapping(value = "/employeeList", method = RequestMethod.GET)
public  @ResponseBody ArrayList<Employee> getAllEmployee() {



    System.out.println("-------------- inside getUsers() ...");

    ArrayList<Employee> employee_List = (ArrayList<Employee>) employeeServices.getAllEmployee();

    return employee_List;

}



}

dao文件是:

public interface EmployeeDao {
public void add(Employee employee);
public void edit(Employee employee);
public void delete(String empNumer);
public Employee getEmployee(String empNumer);
public List getAllEmployee();

}

daoimpl.java是

@Repository

public class EmployeeDaoImpl implements EmployeeDao {

    @Autowired
    private SessionFactory session;

    @Override
    public void add(Employee employee) {

        session.getCurrentSession().save(employee);

    }

    @Override
    public void edit(Employee employee) {
        session.getCurrentSession().update(employee);

    }

    @Override
    public void delete(String empNumer) {
        session.getCurrentSession().delete(getEmployee(empNumer));

    }

    @Override
    public Employee getEmployee(String empNumer) {
        return (Employee)session.getCurrentSession().get(Employee.class, empNumer);
    }

    @Override
    public List getAllEmployee() {
        return session.getCurrentSession().createQuery("from employee_detail").list();
    }


}

hibernate.cfg.xml是: -

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 

<hibernate-configuration>
    <session-factory>     

     <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>

        <!-- Assume hibernateTutorial is the database name -->
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/roster_app</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">root</property>
        <property name="hibernate.current_session_context_class"> org.springframework.orm.hibernate3.SpringSessionContext
</property>
        <property name="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
        <property name="hibernate.c3p0.idle_test_period">3600</property>
        <property name="hibernate.c3p0.timeout">28800</property>   
        <mapping class="com.lak.model.Employee" />

    </session-factory>         
</hibernate-configuration>

如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

微米。 Deinum的评论是正确的。您需要使用HQL。在没有看到你的ORM模型但是你的hibernate.cfg.xml判断你有一个名为Employee的ORM类,这让我相信Employee_detail是你数据库中Employee表的一个列。它是否正确?如果是这样你想要的是类似于:

的HQL查询

SELECT employee_detail FROM Employee

将为您提供employee_detail列的结果。 如果您只是想要一个Employee对象列表,那么您希望查询只是:

FROM Employee

将为您提供Employee对象列表,每个对象都有自己的字段,用于数据库表中的任何列。看看这个方法:

@Override
public List getAllEmployee() {
    return session.getCurrentSession().createQuery("from Employee").list();
}