簡體   English   中英

如何檢查數據庫中是否已存在電子郵件-JPA,Java,JBoss

[英]How to check if email already exists in DB - JPA, Java, JBoss

我正在嘗試檢查用戶輸入的電子郵件是否已存在於數據庫中。 我必須在UserBean.java中使用方法(注冊和更新)和一個輔助方法:

    public String register() {
        if (emailAlreadyExists(this.user.getEmail())) {
            FacesContext.getCurrentInstance().addMessage(null,
                    new FacesMessage("This e-Mail is already in use."));
        } else {
            this.conversation.end();
            try {
                if (this.id == null) {
                    this.entityManager.persist(this.user);
                    return "thanks.xhtml";
                } else {
                    this.entityManager.merge(this.user);
                    return "thanks.xhtml";
                }
            } catch (EJBTransactionRolledbackException e) {
                Throwable t = e.getCause();
                while ((t != null)
                        && !(t instanceof ConstraintViolationException)) {
                    t = t.getCause();
                }
                if (t instanceof ConstraintViolationException) {
                    FacesContext.getCurrentInstance().addMessage(null,
                            new FacesMessage(e.getMessage()));
                    return null;
                }
            }
        }
        return null;
    }

    public String update() {
            if (emailAlreadyExists(this.user.getEmail())) {
                FacesContext.getCurrentInstance().addMessage(null,
                        new FacesMessage("This e-Mail is already in use."));
            } else {
                this.conversation.end();
                try {
                    if (this.id == null) {
                        this.entityManager.persist(this.user);
                        return "search?faces-redirect=true";
                    } else {
                        this.entityManager.merge(this.user);
                        return "view?faces-redirect=true&id=" + this.user.getId();
                    }
                } catch (Exception e) {
                    FacesContext.getCurrentInstance().addMessage(null,
                            new FacesMessage(e.getMessage()));
                    return null;
                }
            }
            return null;
        }

public boolean emailAlreadyExists(String emailAddr) {
        long counter = 0;
        counter = (Long) this.entityManager
                .createQuery(
                        "SELECT COUNT(u.email) FROM User u WHERE u.email = :email")
                .setParameter("email", emailAddr).getSingleResult();
        if (counter > 0) {
            return true;
        }
        return false;
    }

我不知道為什么,但是此代碼非常適合register方法(無論何時輸入現有的電子郵件地址),但不適用於update方法,盡管它們非常相似。 每次執行用戶更新時,這些SQL語句均按以下順序執行(應該相反):

22:44:20,865 INFO  [stdout] (http-localhost-127.0.0.1-8080-1) Hibernate: update User set  country=?, email=?, firstName=?, lastName=?, password=?, phone=?, postalCode=? where id=?
22:44:20,875 INFO  [stdout] (http-localhost-127.0.0.1-8080-1) Hibernate: select count(user0_.email) as col_0_0_ from User user0_ where user0_.email=? limit ?

我已經調試了代碼,並認識到該更新語句是在該行執行的:

counter = (Long) this.entityManager
            .createQuery(
                    "SELECT COUNT(u.email) FROM User u WHERE u.email = :email")
            .setParameter("email", emailAddr).getSingleResult();

任何幫助將不勝感激。

您有一個陳舊的實體,該實體是JPA和您的會話bean之間的交易噩夢造成的。 您必須更改用戶實體生命周期,並在JPA事務管理上進行適當的更改。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM