不同的输出,相同的代码Hibernate

时间:2016-11-01 10:56:09

标签: java hibernate

这是我的代码:

public boolean isValidAccount(Account a) {
    boolean valid = false;
    EntityTransaction tx = AbstractService.getEntityTransaction();
    try {
        tx.begin();
        if (AbstractService.getEntityManager().createQuery("from Account a where a.naam = :naam")
                .setParameter("naam", a.getNaam()).getSingleResult() != null) {
            valid = true;
            return valid;
        }
        tx.commit();

    } catch (Exception ex) {
        tx.rollback();
    }
    return valid;
}

当我这样做时:

public static void main(String[] args) {
    AccountService as = new AccountService();
    Account testAccount = new Account();
    testAccount.setNaam("a");
    testAccount.setWachtwoord("a");
    boolean yes = as.isValidAccount(testAccount);
    System.out.println(yes);
    boolean yes2 = as.isValidAccount(testAccount);
    System.out.println(yes2);
    boolean yes3 = as.isValidAccount(testAccount);
    System.out.println(yes3);
}

输出结果为:

true
false
true

假设我的数据库包含一个名称为a的帐户:a和密码a

这怎么可能发生?

0 个答案:

没有答案