0

当我尝试搜索 mysql 的值时出现错误

这是小服务程序

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

这是所有获取和设置时的登录类

@Entity

公共类登录实现可序列化{私有静态最终长序列版本UID = 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)
4

2 回答 2

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()返回实体的实例,而不是字符串,因此上面的行根本没有意义。

一个正确的方法是:

/**
 * Loads and returns the Login entity identified by the given identifier 
 */
public Login find(long id) {
    return (Login) session.load(Login.class, id);
}

您的代码中还有其他大问题,例如在每次请求时读取配置和初始化 Hibernate,而不是只执行一次。

阅读手册。

于 2013-07-08T18:10:01.510 回答
0

我相信您需要将Login对象的类型 id传递Longload()方法,而不是类型Login为 named的对象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;
    }

纠正这个问题

public String find(Long id) {
    Login login = null;
    try {

        //Passing the actual id instead of the object
        login = (String) session.load(Login.class, id);

    } catch (Exception e) {
        e.printStackTrace();
    }
        return login;
}
于 2013-07-08T18:07:11.950 回答