java 如何在提交使用 Hibernate 创建用户之前检查数据库中是否存在用户名?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6516123/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 16:10:16  来源:igfitidea点击:

How to check user name if exist in database before committing to create user with Hibernate?

javahibernategwt

提问by quarks

Hi there is a simple login service I made with GWT, Hibernate & HSQLDB.

嗨,我用 GWT、Hibernate 和 HSQLDB 制作了一个简单的登录服务。

Right now I can create accounts, however, I need to add a functionality that will check if the username is already in the database before committing to the database.

现在我可以创建帐户,但是,我需要添加一个功能,在提交到数据库之前检查用户名是否已经在数据库中。

LoginServiceImpl.java

登录服务实现程序

@Override
public void createAccount(Account user) {

    try {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        session.save(user);
        session.getTransaction().commit();
    }catch (HibernateException e) {
        e.printStackTrace();
    }catch (InvocationException e) {
        e.printStackTrace();

    }
}

Account.java

账号.java

public class Account implements Serializable {
      Long id;
      String name;
      String password;
  public Account() {
      }
      public Account(Long id) {
        this.id = id;
      }
      public String getPassword() {
        return password;
      }
      public void setPassword(String password) {
        this.password = password;
      }
      public Long getId() {
        return id;
      }
      public String getName() {
        return name;
      }
      public void setName(String name) {
        this.name = name;
      }
      public void setId(Long id) {
            this.id = id;
      }
}

回答by Adisesha

Query query=  session.createQuery("from Account where name=?");

Account user=(Account)query.setString(0,user.getName()).uniqueResult();
if(user!=null){
   //Do whatever you want to do
}else{
  //Insert user
} 

回答by Brad Mace

I'd add something like this to your Accountclass. This ensures that the check is always performed when the model is saved, instead of having to manually add the check to every piece of code that saves the model.

我会在你的Account课堂上添加这样的东西。这确保在保存模型时始终执行检查,而不必手动将检查添加到保存模型的每段代码中。

@PrePersist
public void prepareToInsert() {
    List<User> conflicts = find("user=?").fetch();
    if (!conflicts.isEmpty()) {
         throw new IllegalArgumentException("username `" + name + "` is already taken");
    }
}

回答by amod

write the logic of checking already available user in set method and it should return an exception when user already exists. however i am very new to hibernate lets see whether any expert have a simple solution.

在 set 方法中编写检查已可用用户的逻辑,当用户已存在时应返回异常。但是我对 hibernate 很陌生,让我们看看是否有专家有一个简单的解决方案。