我是java新手,我正在通过spring与hibernate集成创建一个与数据库交互的登录页面。我使用了两个类Controler和Dao。我在Hms pojo类中使用了hibernate注释来与数据库进行交互。
我在controller1类中登录方法时遇到类强制转换异常。有人可以让我知道我哪里出错了。
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");
}
}
堆栈跟踪:
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)
答案 0 :(得分:1)
您的查询返回对象数组,您无法调用类的getter方法,直到将其转换为该类类型为止 你可以试试这个
Query q = session.createQuery(sql);
List<Object[]> li= (List<Object[]>)q.list();
for(Object[] ob: li)
{
user=ob[0].toString();
Pass=ob[1].toString();
}