0

I need to verify the email of the new user who would like to sign up in my application web. if the email is already in my database (mysql) so must don't accept this sign up and said said to him like: "your email already used". Now I can save users in my database, but how to check them by his email for not repeat the inscription in my application web.

this is my Dao layer class :

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

}

this is my user 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));

    }

}

And I created a table "user" in my database.

Maybe there is an annotation which can help us to specify the email property as an unique one or something else.

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
  • Checking in your DAO layer is one thing (for graceful exception handling) but you should also add a unique constraint to enforce integrity on the lowest level possible. – Sebastian S Jul 12 '15 at 14:38
  • 1
    You could simply use a UNIQUE INDEX an the email field, or if you want the programmatic way: `BEGIN TRANSACTION, select count(*) from users where email = ?, if result > 0: throw exception, else: INSERT new user, END TRANSACTION`. – Benjamin M Jul 12 '15 at 14:39
  • @SebastianS can you please explain more or if you have an answer you can tell how to do it. Thanks for your comment –  Jul 12 '15 at 14:42
  • 1
    See Luiggi's answer, this is basically what you want. When you try to insert an already-existing email, a ConstraintViolationException will be thrown. All you have to do is check the cause of this exception and display an "email already exists" message. Other than check-then-insert approaches the validation is done by the database itself, so you can not _accidentally_ bypass your check. – Sebastian S Jul 12 '15 at 14:46
  • @SebastianS : read my comment of his answer. Maybe something wrong with me –  Jul 12 '15 at 15:06
  • @SebastianS all right , I fixed the problem I need just to how can I display the "email is already used " as message while launching of this exception –  Jul 12 '15 at 15:32
  • @lee you have to analyze the root cause of the exception. [This answer](http://stackoverflow.com/a/3508976/4014509) should help you. However keep in mind, that exceptions vary from database to database, so this may reduce your portability. – Sebastian S Jul 12 '15 at 15:40
  • @SebastianS : thanks so much for your help I fixed it I just need to add try catch in my save method and catch the constrain exception so modify my insert method like : public void insert(User user) { try { openSession(); User p = new User(user.getName(), user.getEmail(), user.getPassword()); session.save(p); System.out.println("sauvegarde reussi"); closeSession(); } catch (ConstraintViolationException e) { System.out.println("> email is already used"); } } –  Jul 12 '15 at 15:45

1 Answers1

2

What you can do is create a unique key on your email column in your table. After that, decorate your field using @Column(unique=true), that will indicate to Hibernate that this field has a unique key.

Also, be careful with your annotations. This is unrelated to your problem, but @ManagedBean marks the class as a bean able to interact with the view in JSF. Probably you want/need to use @Entity instead.

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
  • I did what you told me . 1- edit table user like : Alter table user add unique (email) 2- and I added the annation in my email property like @Column(unique=true) private String email; but it still save duplicate email in my database –  Jul 12 '15 at 15:02
  • 1
    I don't think you're using the same database/table. Once you create an unique key, then any insert/update with a duplicate element on that column should raise an error in the execution of the statement. You're doing something wrong. In order to see if your table has a unique key for your column, use `SHOW CREATE TABLE ` to see the DDL to generate your table, there must be the unique key constraint. – Luiggi Mendoza Jul 12 '15 at 15:19
  • you're right I fixed this one . I add the same email and an exception launched, I have to do is check the cause of this exception and display an "email already exists" message. how to do that please ? any idea –  Jul 12 '15 at 15:29
  • That's a different question. I would recommend you to seek for handling exceptions. Here's the [official Java tutorial](https://docs.oracle.com/javase/tutorial/essential/exceptions/handling.html) on this, and after that you have to adapt what you've learnt into your project. – Luiggi Mendoza Jul 12 '15 at 15:31
  • I just need to add try catch in my save method and catch the constrain exception so modify my insert method like : public void insert(User user) { try { openSession(); User p = new User(user.getName(), user.getEmail(), user.getPassword()); session.save(p); System.out.println("sauvegarde reussi"); closeSession(); } catch (ConstraintViolationException e) { System.out.println("> email is already used"); } } –  Jul 12 '15 at 15:40
  • You certainly can catch ConstraintViolationException and display a generic warning like "could not be saved". However this exception may also be thrown for different reasons. It is hard to distinguish different root causes of a CVE. See linked answer in my comment on your question, if you want to display a more precise warning like "email already taken". – Sebastian S Jul 12 '15 at 15:42