elunez/eladmin · error · EntityExistException

User with phone {} existed

Error message

User with phone {} existed

What it means

EntityExistException thrown in UserServiceImpl.create when the phone number is already used by another user. Third and last uniqueness gate in create (after username and email); findByPhone returning any row rejects the new account.

Source

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

        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());
        }
        if (user2 != null && !user.getId().equals(user2.getId())) {
            throw new EntityExistException(User.class, "email", resources.getEmail());

View on GitHub (pinned to 55fbf70595)

Solutions

  1. Assign a distinct phone number to the new account.
  2. Update or delete the old account that holds the number first.
  3. Deduplicate phone numbers in import spreadsheets before running the import.

Example fix

// before
user.setPhone("13800138000"); // classic dummy, already used

// after
user.setPhone("13900001111");
Defensive patterns

Strategy: validation

Validate before calling

if (userRepository.findByPhone(form.phone) != null) { errors.phone = '手机号已被使用'; return; }

Type guard

const isUnique = (p: string, users: {phone:string}[]) => !users.some(u => u.phone === p);

Try / catch

catch (EntityExistException e) { if (e.getMessage().startsWith("User with phone")) errors.phone = '手机号已被占用'; }

Prevention

When it happens

Trigger: POST /api/users with a mobile number that exists on any other account.

Common situations: Ex-employees' accounts kept active while re-onboarding the same person; test accounts created with dummy phone '13800138000' colliding; bulk imports with duplicated phone columns.

Related errors


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