Here's what I have, which has problems:
public User addUser(final String name, final String uid) {
final List<User> result = findByProperty("uid", uid);
if (!result.isEmpty())
return (result.get(0));
final User user = new User(name, uid);
saveOrUpdate(user);
return (user);
}
where findByProperty is using a Criteria and adding
Restrictions.eq(propertyName, propertyValue))
The uid in this case is not the table's primary key, it's from another system (ldap), although it is unique.
The main problem that I see with my code, and there may be others, is that there's window of opportunity for a collision, between the check for existence and the saveOrUpdate.
Thanks for your help.