elunez/eladmin · error · EntityExistException
Role with username {} existed
Error message
Role with username {} existed What it means
EntityExistException thrown in RoleServiceImpl.create when a role with the same name already exists. findByName is checked before save; role names are unique. The message template says 'username' because EntityExistException formats the field name verbatim — the actual field is the role name, a known cosmetic quirk of this call.
Source
Thrown at eladmin-system/src/main/java/me/zhengjie/modules/system/service/impl/RoleServiceImpl.java:97
}
@Override
public RoleDto findById(long id) {
String key = CacheKey.ROLE_ID + id;
Role role = redisUtils.get(key, Role.class);
if (role == null) {
role = roleRepository.findById(id).orElseGet(Role::new);
ValidationUtil.isNull(role.getId(), "Role", "id", id);
redisUtils.set(key, role, 1, TimeUnit.DAYS);
}
return roleMapper.toDto(role);
}
@Override
@Transactional(rollbackFor = Exception.class)
public void create(Role resources) {
if (roleRepository.findByName(resources.getName()) != null) {
throw new EntityExistException(Role.class, "username", resources.getName());
}
roleRepository.save(resources);
}
@Override
@Transactional(rollbackFor = Exception.class)
public void update(Role resources) {
Role role = roleRepository.findById(resources.getId()).orElseGet(Role::new);
ValidationUtil.isNull(role.getId(), "Role", "id", resources.getId());
Role role1 = roleRepository.findByName(resources.getName());
if (role1 != null && !role1.getId().equals(role.getId())) {
throw new EntityExistException(Role.class, "username", resources.getName());
}
role.setName(resources.getName());
role.setDescription(resources.getDescription());
role.setDataScope(resources.getDataScope());View on GitHub (pinned to 55fbf70595)
Solutions
- Pick a different role name, or reuse the existing role instead of creating a new one.
- Delete the existing role first (after clearing its user associations, see the role verification error).
- Check GET /api/roles before POST to make scripts idempotent.
Example fix
// before
roleService.create(new Role("HR")); // name taken
// after
if (roleRepository.findByName("HR") == null) {
roleService.create(new Role("HR"));
} Defensive patterns
Strategy: validation
Validate before calling
if (roleRepository.findByName(form.name) != null) { errors.name = '角色名称已存在'; return; } Try / catch
catch (EntityExistException e) { // 'Role with username ... existed' actually means role NAME taken formErrors.name = '名称已存在'; } Prevention
- Note the misleading 'username' wording in the message — it is the role name
- Deduplicate role names in seed/SQL scripts
- Check the role list before creating
When it happens
Trigger: POST /api/roles with a name that matches an existing role (e.g. creating a second '超级管理员' or re-adding 'HR').
Common situations: Re-running role seed data; two admins creating the same role concurrently; sync scripts re-importing roles without idempotence.
Related errors
- Menu with title {} existed
- User with username {} existed
- User with email {} existed
- User with phone {} existed
- A new role cannot already have an ID
AI-assisted analysis of elunez/eladmin@55fbf70595 (2026-08-14).
Data as JSON: /api/errors/2308dd80054f703a.
Report an issue: GitHub.