我将检查数据库中不存在的电子邮件
,然后使用其他属性存储该行。
这是我的代码,可以检测重复的email
值:
public boolean exists(String value) {
customerDao.openSessionWithTransaction();
Query query = customerDao.openSession().createQuery("from Customers where email=? ");
query.setString(0, value);
if (query.getFirstResult() != null) {
return true;
}
return false;
}
但 getFirstResult()
始终返回 null
,即使我检查重复的 email
值也是如此。
我已向 pojo
类声明了 uniuness
注释。
这是我的表格:
当我检查重复的电子邮件值时,只会发生 ConstraintViolationException
。
最佳答案
您可能需要这个:
public boolean exists(String value) {
customerDao.openSessionWithTransaction();
Query query = customerDao.openSession().createQuery("from Customers where email=? ");
query.setString(0, value);
if (query.uniqueResult()!= null) {
return true;
}
return false;}
关于java - 检测 Hibernate 中的重复行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28974689/