0

i am very new to hibernate and trying to map java class to table using hibernate annotations

my pojo (Entity) class looks like this

    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 + "]";
    }





}

my table name is 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    |                |
+-----------+-------------+------+-----+---------+----------------+

i have mapped everything properly to the best my knowledge ,but still i am getting this following exception

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 file

<!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 factory

  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;   
    }
}

and implementation

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 look at my project structure please check it .cfg file location

please help me out to solve this thank you.

bharath
  • 387
  • 1
  • 11
  • 30

3 Answers3

0

HQL uses the names defined in your Java Class, instead of the SQL table names you defined.

Therefore, the table is called StudentLogin in your HQL query instead of studentdetails, so swapping these names should work.

also pdwl should be password in your query as well.

0

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

should be modified to

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

Hibernate mappers Class name, not table name.

Bejond
  • 1,188
  • 1
  • 11
  • 18
0

i apologize every one for my mistake and got the mistake what i have done.

Actually my project is spring MVC project .

1.i did not configure hibernate connection pool in spring-servlet.xml file 2. i did not Setup Hibernate session factory

and hibernate relate configurations i did not mention in the file.

need add all congurations related to hibernate in spring-servlet.xml file

bharath
  • 387
  • 1
  • 11
  • 30