使用hibernate注册时,检查我的数据库中是否有电子邮件

时间:2015-07-10 11:14:46

标签: java mysql hibernate dao

我正在编写IHM来注册用户,我需要检查这个用户是否已经在数据库(mysql)中,通过他的电子邮件进行检查。你能帮我吗。 我现在可以保存我的用户,但如何通过他的电子邮件检查该用户

这是我的Dao图层类:

public class UserDaoMysql implements UserDao {

    private Session session;


    private void openSession(){
        SessionFactory sessionFactory=HibernateUtil.getSessionFactory();
        session = sessionFactory.openSession();
        session.beginTransaction();
    }

    private void closeSession(){
        session.getTransaction().commit();
        session.close();
    }

    public void insert(User user) {

        if(checkEmail(user)){

        openSession();
        User p = new User(user.getName(), user.getEmail(), user.getPassword());
        session.save(p);
        System.out.println("sauvegarde reussi");
        closeSession();

        }



    }

    public boolean checkEmail(User user){


        return true;
    }

}

这是我的用户bean:

@ManagedBean(name="user")
public class User {

    private int id;
    private String name;
    private String email;
    private String password;
    private String confirmationPass;
//  private image 


    public User() {
        super();
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }

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

    public String getPassword() {
        return password;
    }

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


    public String getConfirmationPass() {
        return confirmationPass;
    }

    public void setConfirmationPass(String confirmationPass) {
        this.confirmationPass = confirmationPass;
    }


    public User(int id, String name, String email, String password,
            String confirmationPass) {
        super();
        this.id = id;
        this.name = name;
        this.email = email;
        this.password = password;
        this.confirmationPass = confirmationPass;
    }

    public User(int id, String name, String email, String password) {
        super();
        this.id = id;
        this.name = name;
        this.email = email;
        this.password = password;
    }

    public User(String name, String email, String password) {
        super();
        this.name = name;
        this.email = email;
        this.password = password;
    }

    @Override
    public String toString() {
        return "User [id=" + id + ", Name=" + name + ", email=" + email
                + ", password=" + password + "]";
    }

    public void save(){


        UserBusiness userBusiness = new UserBusinessImp();
        userBusiness.add(new User(name, email,password));

    }

}

我的数据库中有一个表用户。

感谢您提前的帮助

1 个答案:

答案 0 :(得分:1)

我会使用NamedQuery。在您的User实体中定义命名查询,如下所示:

...
@NamedQueries({
    @NamedQuery(name = "User.findByEmail", 
query = "SELECT u FROM User u WHERE u.email = :email")})
@ManagedBean(name="user")
public class User {
...

然后将这样的方法添加到您的DAO

public List<User> getUsersByEmail(String email){
    openSession();
    Session session;
    Query query = session.getNamedQuery("User.findByEmail");
    query.setString("email", email);
    Lis<Users> users = query.list();
    closeSession();
    return users;
}

此方法稍微通用一些,您可以使其更具体,仅返回user计数。