0
votes

I have a error when I try search a value to mysql

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

this is the class Login when have all get and set

@Entity

public class Login implements 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;
}

and this is the class LoginDAO when have connect to mysql

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

whe I run the application have this error

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)
2

2 Answers

1
votes

The error message is self-explanatory:

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

This means that, when you're loading a Login by ID in the following line

login = (String) session.load(Login.class, id);

you pass a Login instance (id) as argument, instead of passing an instance of the type of the ID of the Login entity: Long.

Moreover, Session.load() returns an instance of the entity, and not a String, so the above line makes no sense at all.

A correct method would be:

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

You have other big problems in your code, like reading the configuration and initializing Hibernate at each request, instead of doing it only once.

Read the manual.

0
votes

I believe you need to pass the Login object's id of type Long into the load() method instead of the object of type 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;
    }

To correct this issue

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