Problem with HQL for data reading


#1

Hello everybody!

I trying to create authorisation algorithm, based on comparison between values, inputed from jsp-page and values, extracted from database. If values (login and password) are equal, going to page “/newsPage.jsp” must be implemented, else - going to page “/loginPage.jsp”.
But no matter what i inputted (equaled or not equaled values), going to page “/loginPage.jsp” is implemented.
I supposed, that problem is focused in query-expressions: for unknown reason string data don’t expressed from database properly.
Database contain only one row with id = 1.
Additionally: with starting Debug i received ClassNotFoundException: com.intellij.rt.debugger.agent.CaptureAgent.

  String login = request.getParameter("login");
        String password = request.getParameter("password");
 int count = 1;
          //  int count2;

            // Data extaction

            Query etalonlogin2 = factoryClass.getSessionFactory().
                    openSession().createQuery("from Logindata where id=:counter").
                    setParameter("counter", count);

            List<Logindata> logindataList = etalonlogin2.getResultList();

            for (Logindata logindata : logindataList) {
                etalonlogin = logindata.getLogin();
            }

            Query etalonpassword2 = factoryClass.getSessionFactory().
                    openSession().createQuery("from Logindata where id=:counter").
                    setParameter("counter", count);

            List<Logindata> passworddataList = etalonpassword2.getResultList();


            for (Logindata logindata : passworddataList) {
                etalonpassword = logindata.getPassword();
            }

            //compare of data, which being extracted and inputed

            if (etalonlogin.equals(login) & etalonpassword.equals(password)) {

                if(response.isCommitted()) {
                    response.sendRedirect("/newsPage.jsp");
                }
            } else {
                returnPage = "/loginPage.jsp";
                getServletContext().getRequestDispatcher(returnPage).forward(request, response);
                doGet(request,response);
            }  

I would be grateful for any help!


#2

There are many issues in this example:

  • web logic mixed with data access logic
  • Hibernate Sessions created on-the-fly just to execute a query
  • the question is rather vague and doesn’t provide a clear explanation of the actual issue

After you read the Hibernate User Guide, you are going to get a better understanding of some of these issues and fix them accordingly.


#3

Thank you for your comments.
And I have found, that query in this code works correctly - in terms of that it extracts correctly data from base. Therefore, the problem is focused in web-logic, which provides the redirect of resources (response.sendRedirect).
Following your advices, I will modify the code, through creating new classes for query-services.
But problem in switchings between servlets will keeps current. Is it matter of this - hibernate - forum, or it will better for me to asking for advice (about servlets) to other communities?


#4

Is it matter of this - hibernate - forum, or it will better for me to asking for advice (about servlets) to other communities?

Since the problem is not related to Hibernate, you are better off asking it on a different forum.