org.hibernate.hql.ast.QuerySyntaxException:表未映射(但已映射)

时间:2018-04-13 11:52:02

标签: java hibernate

我是hibernate的新手,尝试使用hibernate注释将java类映射到表

我的pojo(Entity)类看起来像这样

    package com.rasvek.smartpos.beans;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;


@Entity
@Table(name="studentdetails")
public class StudentLogin 
{

    public StudentLogin() {

    }

    public StudentLogin(String userName, String password, int id) {
        //super();
        this.userName = userName;
        this.password = password;
        this.id = id;
    }
    @Column(name="pwdl")
    private String password;

    @Id  
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="studentId")
    private int id;

    @Column(name="userName")
    private String userName;



    public String getUserName() {
        return userName;
    }
    public void setUserName(String userName) {
        this.userName = userName;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }


    public String getPassword() {
        return password;
    }  
    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String toString() {
        return "StudentLogin [userName=" + userName + ", password=" + password + ", id=" + id + "]";
    }





}

我的表名是studentdetails

+-----------+-------------+------+-----+---------+----------------+
| Field     | Type        | Null | Key | Default | Extra          |
+-----------+-------------+------+-----+---------+----------------+
| studentId | int(11)     | NO   | PRI | NULL    | auto_increment |
| userName  | varchar(45) | YES  |     | NULL    |                |
| pwdl      | varchar(45) | YES  |     | NULL    |                |
+-----------+-------------+------+-----+---------+----------------+

我已经根据我的知识正确地映射了一切,但我仍然得到以下异常

org.hibernate.hql.ast.QuerySyntaxException: studentdetails is not mapped [from studentdetails where userName = :userName and pwdl= :pwdl]
at org.hibernate.hql.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:181)
at org.hibernate.hql.ast.tree.FromElementFactory.addFromElement(FromElementFactory.java:110)
at org.hibernate.hql.ast.tree.FromClause.addFromElement(FromClause.java:94)
at org.hibernate.hql.ast.HqlSqlWalker.createFromElement(HqlSqlWalker.java:316)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3228)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:3112)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:720)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:571)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:288)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:231)
at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:254)
at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:185)
at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:136)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:101)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:80)
at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:94)
at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:156)
at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:135)
at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1651)
at com.rasvek.smartpos.hibernate.dao.impl.SpringFormLoginDAOImpl.authencateUser(SpringFormLoginDAOImpl.java:29)
at com.rasvek.smartpos.controller.SpringFormLoginController.authencateUser(SpringFormLoginController.java:27)
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.doInvoke(InvocableHandlerMethod.java:221)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:777)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:706)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:943)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:877)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:868)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:292)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:528)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1100)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:687)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1520)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1476)
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)

.cfg文件

<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

    <session-factory>

        <!-- JDBC Database connection settings -->
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/hb_student_tracker?useSSL=false</property>
        <property name="connection.username">hbstudent</property>
        <property name="connection.password">hbstudent</property>

        <!-- JDBC connection pool settings ... using built-in test pool -->
        <property name="connection.pool_size">1</property>  

        <!-- Select our SQL dialect -->
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>

        <!-- Echo the SQL to stdout -->
        <property name="show_sql">true</property>

        <!-- Set the current session context -->
        <property name="current_session_context_class">thread</property>

    </session-factory>

</hibernate-configuration>

sessesion工厂

  package com.rasvek.smartpos.hibernate.factory;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class SpringFormsSessionFactory 
{

    private static SessionFactory sessionFactory;
    public static Configuration cfg;

    static{

        sessionFactory = new Configuration().configure("hibernateSpringForm.cfg.xml").buildSessionFactory();
         }
    /**
     * @return the sessionFactory
     */
    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    /**
     * @param sessionFactory the sessionFactory to set
     */
    public static void setSessionFactory(SessionFactory sessionFactory) 
    {
        SpringFormsSessionFactory.sessionFactory = sessionFactory;   
    }
}

和实施

package com.rasvek.smartpos.hibernate.dao.impl;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;



import com.rasvek.smartpos.beans.StudentLogin;
import com.rasvek.smartpos.hibernate.dao.inter.SpringFormLoginDAOInter;
import com.rasvek.smartpos.hibernate.factory.SmartPOSSessionFactory;
import com.rasvek.smartpos.hibernate.factory.SpringFormsSessionFactory;

public class SpringFormLoginDAOImpl implements SpringFormLoginDAOInter 
{
    Session session=null;
    public boolean authencateUser(String userName, String password)  
    {
        session = SpringFormsSessionFactory.getSessionFactory().openSession();
        boolean userStatus=false;
        try {    

            if(userName !=null && password !=null)
            {

                System.out.println("before");
                List<StudentLogin>  list=session.createQuery("from studentdetails where userName = :userName and pwdl= :pwdl").setParameter(1, userName)
                        .setParameter(2, password).list();   
                    if(!list.isEmpty())   
                    {         
                        userStatus=true;
                    }

                    System.out.println("after");

                    }  
        }  

    catch (Exception e) {
        e.printStackTrace();
    }
        finally {
            if(session!=null)
            session.close();
        }

        return userStatus;
    }

}

请查看我的项目结构 please check it .cfg file location

请帮我解决这个问题。

3 个答案:

答案 0 :(得分:0)

HQL使用Java类中定义的名称,而不是您定义的SQL表名。

因此,该表在您的HQL查询中称为StudentLogin而不是studentdetails,因此交换这些名称应该有效。

您的查询中pdwl也应该是password

答案 1 :(得分:0)

from studentdetails where userName = :userName and pwdl= :pwdl

应修改为

from StudentLogin where userName = :userName and pwdl= :password

Hibernate mappers类名,而不是表名。

答案 2 :(得分:0)

我为我的错误向每个人道歉并弄错了我所犯的错误。

实际上我的项目是spring MVC project

1. 我没有在spring-servlet.xml文件中配置hibernate connection pool 2. 我没有设置Hibernate会话工厂

和hibernate关联我在文件中没有提到的配置。

需要在spring-servlet.xml文件中添加与hibernate相关的所有配置