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

  1. Choose a different username.
  2. If the intent is to restore an old account, update the existing user instead of creating a new one.
  3. 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

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


AI-assisted analysis of elunez/eladmin@55fbf70595 (2026-08-14). Data as JSON: /api/errors/d0c35734941656bc. Report an issue: GitHub.