如何检查数据库中的重复条目?
问题描述:
我需要进行检查,以使用户无法使用数据库中已经存在的电子邮件ID进行注册.
I need to apply a check so that a user cannot register using an email id which already exists in the database.
答
大概是这种DAO方法:
Probably something like this DAO method :
public boolean isDuplicateEntry(String email) {
Session session = getSession();
try {
User user = (User) session.get(User.class, email);
session.close();
return (null != user);
} catch (RuntimeException e) {
log.error("get failed", e);
session.close();
throw e;
}
}