elunez/eladmin · error · EntityExistException
User with username {} existed
Error message
User with username {} existed What it means
EntityExistException thrown in UserServiceImpl.create when the username is already taken. findByUsername is the first of three uniqueness checks (username, email, phone) executed before save. Usernames are the login identifier, so duplicates are always rejected.
Source
Thrown at eladmin-system/src/main/java/me/zhengjie/modules/system/service/impl/UserServiceImpl.java:89
@Override
@Transactional(rollbackFor = Exception.class)
public UserDto findById(long id) {
String key = CacheKey.USER_ID + id;
User user = redisUtils.get(key, User.class);
if (user == null) {
user = userRepository.findById(id).orElseGet(User::new);
ValidationUtil.isNull(user.getId(), "User", "id", id);
redisUtils.set(key, user, 1, TimeUnit.DAYS);
}
return userMapper.toDto(user);
}
@Override
@Transactional(rollbackFor = Exception.class)
public void create(User resources) {
if (userRepository.findByUsername(resources.getUsername()) != null) {
throw new EntityExistException(User.class, "username", resources.getUsername());
}
if (userRepository.findByEmail(resources.getEmail()) != null) {
throw new EntityExistException(User.class, "email", resources.getEmail());
}
if (userRepository.findByPhone(resources.getPhone()) != null) {
throw new EntityExistException(User.class, "phone", resources.getPhone());
}
resources.setIsAdmin(false);
userRepository.save(resources);
}
@Override
@Transactional(rollbackFor = Exception.class)
public void update(User resources) throws Exception {
User user = userRepository.findById(resources.getId()).orElseGet(User::new);
ValidationUtil.isNull(user.getId(), "User", "id", resources.getId());
User user1 = userRepository.findByUsername(resources.getUsername());
User user2 = userRepository.findByEmail(resources.getEmail());View on GitHub (pinned to 55fbf70595)
Solutions
- Choose a different username.
- If the intent is to restore an old account, update the existing user instead of creating a new one.
- Make provisioning code idempotent: call GET /api/users/{username} or check existence before POST.
Example fix
// before
userService.create(user); // username taken
// after
if (userRepository.findByUsername(user.getUsername()) == null) {
userService.create(user);
} Defensive patterns
Strategy: validation
Validate before calling
if (userRepository.findByUsername(form.username) != null) { errors.username = '用户名已存在'; return; } Try / catch
catch (EntityExistException e) { if (e.getMessage().startsWith("User with username")) errors.username = '用户名已存在'; } Prevention
- Debounce a username availability check as the admin types
- Treat username as immutable after creation
- Make user-provisioning scripts check existence first
When it happens
Trigger: POST /api/users with a username matching an existing row (e.g. re-adding 'admin' or a recently deleted-but-cached account).
Common situations: Re-running user seed scripts; provisioning integration not checking existence first; admin re-creating a user after a failed import that partially committed.
Related errors
- User with email {} existed
- User with phone {} existed
- Role with username {} existed
- 不能修改他人资料
- 角色权限不足,不能删除:{username}
AI-assisted analysis of elunez/eladmin@55fbf70595 (2026-08-14).
Data as JSON: /api/errors/d0c35734941656bc.
Report an issue: GitHub.