0

我在使用 Hibernate 进行登录过程时遇到问题。所有代码在语法方面都是完全正确的。NetBeans 告诉我我的代码没有问题。但是,当我运行网络并测试登录过程时,它没有反应并且地址卡在 doLogin 上。

所有类都已正确映射。这是我的问题:当我尝试检索数据时,我的代码卡在了一条线上。

在 doLogin servlet 上(我使用 NetBeans 提供的模板,只是在尝试时填写我的代码。这里简要介绍一下:

Connect con = new Connect(); //my code is stucked on this line.
//I've done testing where's the cause of the stuck, and this line is the cause.
List logger = con.getLogin(username, password);

并说清楚:

连接.java

public class Connect {
    Session sesi;

        public Connect() {
            sesi = HibernateUtil.getSessionFactory().openSession();
        }



        public List getLogin(String username, String password){
            return sesi.createQuery("from MsUser WHERE username = '"+username+"' and password = '"+password+"'").list();
        }

    }

由于该查询是 HQL,因此这里是 MsUser 类:

public class MsUser {

    public MsUser() {
    }

    private int userID;
    private String username;
    private String firstname;
    private String lastname;
    private String email;
    private String password;
    private String gender;
    private String address;
    private String phone;
    private String photo;

    public MsUser(int userID, String username, String firstname, String lastname, String email, String password, String gender, String address, String phone, String photo) {
        this.userID = userID;
        this.username = username;
        this.firstname = firstname;
        this.lastname = lastname;
        this.email = email;
        this.password = password;
        this.gender = gender;
        this.address = address;
        this.phone = phone;
        this.photo = photo;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getFirstname() {
        return firstname;
    }

    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getLastname() {
        return lastname;
    }

    public void setLastname(String lastname) {
        this.lastname = lastname;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getPhoto() {
        return photo;
    }

    public void setPhoto(String photo) {
        this.photo = photo;
    }

    public int getUserID() {
        return userID;
    }

    public void setUserID(int userID) {
        this.userID = userID;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }


}

请帮忙。但我怀疑 Connect 的构造函数是主要原因。任何人都可以建议或修复或告诉我是什么导致我这样。

附录:

HibernateUtil.java
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controller;

import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.SessionFactory;

/**
 * Hibernate Utility class with a convenient method to get Session Factory
 * object.
 *
 * @author Ginanjar
 */
public class HibernateUtil {

    private static final SessionFactory sessionFactory;

    static {
        try {
            // Create the SessionFactory from standard (hibernate.cfg.xml) 
            // config file.
            sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
        } catch (Throwable ex) {
            // Log the exception. 
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }
}
4

1 回答 1

0

您可以在 hibernateUtil.java 文件中尝试以下代码:

SessionFactory factory = new Configuration().configure().buildSessionFactory();
        session = factory.openSession();
        String query = "select reg.username,reg.password from MsUser as reg where reg.username='" + username + "' and reg.password='" + password + "'";
        Query DBquery = session.createQuery(query);
        for (Iterator it = DBquery.iterate(); it.hasNext();) {            it.next();
            count++;
        }
        System.out.println("Total rows: " + count);
        if (count == 1) {
            return true;
        } else {
            return false;
        }
    }
于 2015-03-12T10:49:12.090 回答