当我尝试将值搜索到mysql
时出错这是servlet
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// Obtención de la sesión con Hibernate 4.x
Configuration configuration = new Configuration().configure();
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties())
.buildServiceRegistry();
SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
Session session = sessionFactory.openSession();
BuscaCorreo(session);
}
private static void BuscaCorreo(Session session) {
int id = 1;
LoginDAO loginDAO = new LoginDAO(session);
Login login = new Login();
login.setId(id);
loginDAO.find(login);
System.out.println(login.getCorreo());
}
这是所有get和set
时的类Login@Entity
public class Login实现Serializable { private static final long serialVersionUID = 7698531046982304548L;
@Id
private long id;
private String correo;
private String clave;
public Login(){
}
public Login(long id, String correo, String clave) {
super();
this.id = id;
this.correo = correo;
this.clave = clave;
}
public String getCorreo() {
return correo;
}
public void setCorreo(String correo) {
this.correo = correo;
}
public String getClave() {
return clave;
}
public void setClave(String clave) {
this.clave = clave;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
这是连接到mysql时的类LoginDAO
private Session session;
public LoginDAO(Session session) {
this.session = session;
}
public String find(Login id) {
String login = null;
try {
login = (String) session.load(Login.class, id);
} catch (Exception e) {
e.printStackTrace();
}
return login;
}
当我运行应用程序时出现此错误
log4j:WARN No appenders could be found for logger (org.jboss.logging).
log4j:WARN Please initialize the log4j system properly.
org.hibernate.TypeMismatchException: Provided id of the wrong type for class com.utp.soft6.model.enteties.Login. Expected: class java.lang.Long, got class com.utp.soft6.model.enteties.Login
null
at org.hibernate.event.internal.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:132)
at org.hibernate.internal.SessionImpl.fireLoad(SessionImpl.java:1079)
at org.hibernate.internal.SessionImpl.access$2200(SessionImpl.java:172)
at org.hibernate.internal.SessionImpl$IdentifierLoadAccessImpl.getReference(SessionImpl.java:2402)
at org.hibernate.internal.SessionImpl.load(SessionImpl.java:967)
at com.utp.soft6.model.LoginDAO.find(LoginDAO.java:26)
at com.utp.soft6.hola.BuscaCorreo(hola.java:65)
at com.utp.soft6.hola.doGet(hola.java:47)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
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:310)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:722)
答案 0 :(得分:1)
错误消息不言自明:
为类com.utp.soft6.model.enteties.Login提供了错误类型的id。预期:类java.lang.Long,得到类com.utp.soft6.model.enteties.Login
这意味着,当您在以下行中加载按ID登录时
login = (String) session.load(Login.class, id);
您将Login实例(id
)作为参数传递,而不是传递Login实体ID类型的实例:Long。
此外,Session.load()
返回实体的实例,而不是String,因此上面的行完全没有意义。
正确的方法是:
/**
* Loads and returns the Login entity identified by the given identifier
*/
public Login find(long id) {
return (Login) session.load(Login.class, id);
}
您的代码中还有其他大问题,例如在每个请求中读取配置并初始化Hibernate,而不是只执行一次。
阅读手册。
答案 1 :(得分:0)
我认为您需要将Login
对象的Long
类型的ID传递到load()
方法,而不是名为Login
的{{1}}类型的对象。
id
纠正此问题
public String find(Login id) {
String login = null;
try {
/*Next line passes the object(named id), should
pass the actual id (login.id)*/
login = (String) session.load(Login.class, id);
} catch (Exception e) {
e.printStackTrace();
}
return login;
}