elunez/eladmin · error · EntityNotFoundException

User with name {} does not exist

Error message

User with name {} does not exist

What it means

EntityNotFoundException thrown in UserServiceImpl.findByName when no user row matches the username. Unlike getLoginData (which returns null for a missing user), findByName treats the absence as an error — it is the lookup used wherever a username is expected to exist.

Source

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

        delCaches(user.getId(), user.getUsername());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Set<Long> ids) {
        for (Long id : ids) {
            // 清理缓存
            UserDto user = findById(id);
            delCaches(user.getId(), user.getUsername());
        }
        userRepository.deleteAllByIdIn(ids);
    }

    @Override
    public UserDto findByName(String userName) {
        User user = userRepository.findByUsername(userName);
        if (user == null) {
            throw new EntityNotFoundException(User.class, "name", userName);
        } else {
            return userMapper.toDto(user);
        }
    }

    @Override
    public UserDto getLoginData(String userName) {
        User user = userRepository.findByUsername(userName);
        if (user == null) {
            return null;
        } else {
            return userMapper.toDto(user);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updatePass(String username, String pass) {

View on GitHub (pinned to 55fbf70595)

Solutions

  1. Verify the username exists via the user management page or GET /api/users before referencing it.
  2. Restore or re-create the missing account if history must keep resolving its creator.
  3. Where 'missing' is legitimate, switch to getLoginData-style null handling instead of findByName.

Example fix

// before: throws when user deleted
UserDto u = userService.findByName(username);

// after: handle absence explicitly
UserDto u = userService.getLoginData(username);
if (u == null) {
    // treat as anonymous / skip / log
}
Defensive patterns

Strategy: try-catch

Validate before calling

User exists = userRepository.findByUsername(username);
if (exists == null) { /* skip / anonymous / log */ return; }

Try / catch

try { dto = userService.findByName(name); } catch (EntityNotFoundException e) { log.warn("user {} no longer exists", name); dto = null; }

Prevention

When it happens

Trigger: Any internal call resolving a username to a UserDto for a name with no row: e.g. processing data created by a user whose account was deleted, or a JWT whose subject no longer exists.

Common situations: Deleting users while their historical records (creator fields, comments, logs) still reference the name; stale tokens after user cleanup; renaming usernames without updating related references.

Related errors


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