elunez/eladmin · error · EntityExistException

User with email {} existed

Error message

User with email {} existed

What it means

EntityExistException thrown in UserServiceImpl.create when the email is already registered. The email is treated as globally unique across users; findByEmail returning any row aborts creation before the phone check runs.

Source

Thrown at eladmin-system/src/main/java/me/zhengjie/modules/system/service/impl/UserServiceImpl.java:92

    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());
        User user3 = userRepository.findByPhone(resources.getPhone());
        if (user1 != null && !user.getId().equals(user1.getId())) {
            throw new EntityExistException(User.class, "username", resources.getUsername());

View on GitHub (pinned to 55fbf70595)

Solutions

  1. Use a unique email per account (e.g. alias like user+ops@company.com for service accounts).
  2. Free the email first by clearing/changing it on the existing account.
  3. Validate email uniqueness client-side against the user list before submitting.

Example fix

// before
user.setEmail("shared@company.com"); // already registered

// after
user.setEmail("ops-bot@company.com");
Defensive patterns

Strategy: validation

Validate before calling

if (userRepository.findByEmail(form.email) != null) { errors.email = '邮箱已被注册'; return; }

Type guard

const isUnique = (v: string, list: {email:string}[]) => !list.some(u => u.email === v);

Try / catch

catch (EntityExistException e) { if (e.getMessage().startsWith("User with email")) errors.email = '邮箱已被占用'; }

Prevention

When it happens

Trigger: POST /api/users with an email address already present on another user account (corporate shared mailbox reused for two accounts).

Common situations: Creating both a personal and a service account with the same company email; importing HR data where one person had two records; password-reset flows that rely on unique email.

Related errors


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