Join the Stack Overflow Community
Stack Overflow is a community of 6.5 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I am new to java , I am creating a login page to interact with database by spring integrating with hibernate .I am using two classes Controler and Dao. I used hibernate annotations in Hms pojo class for interacting with database .

I am getting class cast exception when comes to login method in controller1 class. Could some one let me know where I went wrong .

Hms.java

package com.hms.project;

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

@Entity
@Table(name="register")
public class Hms {
    private int id;
    @Id
    @GeneratedValue
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    private String firstname;
    private String lastname;
    private String username;
    private String password;
    private String emailid;

    @Column(name="firstname")
    public String getFirstname() {
        return firstname;
    }
    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }
    @Column(name="lastname")
    public String getLastname() {
        return lastname;
    }
    public void setLastname(String lastname) {
        this.lastname = lastname;
    }
    @Column(name="username")
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    @Column(name="password")
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    @Column(name="emailid")
    public String getEmailid() {
        return emailid;
    }
    public void setEmailid(String emailid) {
        this.emailid = emailid;
    }


}

Controler.java

@Controller
public class Controller1 {
    @Autowired
    private Dao dao;


    public Dao getDao() {
        return dao;
    }


    public void setDao(Dao dao) {
        this.dao = dao;
    }


    @RequestMapping("register")
    public void register(Hms hms){
        System.out.println("save data ");

        dao.save(hms);
        System.out.println(" after save data ");
    }
    @RequestMapping("login")
    public String login(String username,String password){ 
        System.out.println("searching for data base");

        List<Hms> l1=dao.listUser(username,password);

        /*String name=req.getParameter("username");
        String pwd=req.getParameter("pwd");*/
        for(Hms l : l1){
            System.out.println("after coming from database");

            System.out.println(l.getUsername());
            System.out.println(l.getPassword());

        /*for(Hms l:obj){

            String s=l.getUsername();
            String s1=l.getPassword();
            if((name==s)&&(pwd==s1)){
                System.out.println("login success");

            }
            else{
                System.out.println("login failed");
            }

        }*/

        //return hms;
    }
        return null;
    }
}

Dao.java

public class Dao {
    private HibernateTemplate hibernatetemplate;

    public HibernateTemplate getHibernatetemplate() {
        return hibernatetemplate;
    }

    public void setHibernatetemplate(HibernateTemplate hibernatetemplate) {
        this.hibernatetemplate = hibernatetemplate;
    }

    public void save(Hms hms){
                          System.out.println("going to database");
                         // hibernatetemplate.save(hms);

    }
     public List<Hms> listUser(String username,String password) {
         System.out.println("going to database");
         String hql="select username,password from Hms";
                    List<Hms> h=hibernatetemplate.find(hql);
                    System.out.println("Fetched data");
                    return h;
        // return hibernatetemplate.find("select username,password from Hms");

}
}

StackTrace:

SEVERE: Servlet.service() for servlet [springs] in context with path [/HmsProject] threw exception [Request processing failed; nested exception is java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to com.hms.project.Hms] with root cause
    java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to com.hms.project.Hms
        at com.hms.project.Controller1.login(Controller1.java:42)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:497)
        at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176)
        at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:440)
        at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:428)
        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)
        at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
        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:502)
        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.runWorker(ThreadPoolExecutor.java:1142)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
        at java.lang.Thread.run(Thread.java:745)
share|improve this question
    
post full stack trace and the code – Omkar Puttagunta Dec 23 '15 at 4:18
up vote 0 down vote accepted

Your query is returning Object array you can't call getter method of your class until you cast it to that class type You can try this

Query q = session.createQuery(sql); 
List<Object[]> li= (List<Object[]>)q.list();
for(Object[] ob: li)
{ 
user=ob[0].toString();
Pass=ob[1].toString();
}
share|improve this answer
    
Thanks buddy , Its working . – pavan Dec 23 '15 at 14:24

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.