{"record":{"id":"2308dd80054f703a","repo":"elunez/eladmin","slug":"role-with-username-existed","errorCode":null,"errorMessage":"Role with username {} existed","messagePattern":"Role with username (.+?) existed","errorType":"exception","errorClass":"EntityExistException","httpStatus":400,"severity":"error","filePath":"eladmin-system/src/main/java/me/zhengjie/modules/system/service/impl/RoleServiceImpl.java","lineNumber":97,"sourceCode":"    }\n\n    @Override\n    public RoleDto findById(long id) {\n        String key = CacheKey.ROLE_ID + id;\n        Role role = redisUtils.get(key, Role.class);\n        if (role == null) {\n            role = roleRepository.findById(id).orElseGet(Role::new);\n            ValidationUtil.isNull(role.getId(), \"Role\", \"id\", id);\n            redisUtils.set(key, role, 1, TimeUnit.DAYS);\n        }\n        return roleMapper.toDto(role);\n    }\n\n    @Override\n    @Transactional(rollbackFor = Exception.class)\n    public void create(Role resources) {\n        if (roleRepository.findByName(resources.getName()) != null) {\n            throw new EntityExistException(Role.class, \"username\", resources.getName());\n        }\n        roleRepository.save(resources);\n    }\n\n    @Override\n    @Transactional(rollbackFor = Exception.class)\n    public void update(Role resources) {\n        Role role = roleRepository.findById(resources.getId()).orElseGet(Role::new);\n        ValidationUtil.isNull(role.getId(), \"Role\", \"id\", resources.getId());\n\n        Role role1 = roleRepository.findByName(resources.getName());\n\n        if (role1 != null && !role1.getId().equals(role.getId())) {\n            throw new EntityExistException(Role.class, \"username\", resources.getName());\n        }\n        role.setName(resources.getName());\n        role.setDescription(resources.getDescription());\n        role.setDataScope(resources.getDataScope());","sourceCodeStart":79,"sourceCodeEnd":115,"githubUrl":"https://github.com/elunez/eladmin/blob/55fbf705956949697dbd68bf9003776609d3d029/eladmin-system/src/main/java/me/zhengjie/modules/system/service/impl/RoleServiceImpl.java#L79-L115","documentation":"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.","triggerScenarios":"POST /api/roles with a name that matches an existing role (e.g. creating a second '超级管理员' or re-adding 'HR').","commonSituations":"Re-running role seed data; two admins creating the same role concurrently; sync scripts re-importing roles without idempotence.","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."],"exampleFix":"// before\nroleService.create(new Role(\"HR\")); // name taken\n\n// after\nif (roleRepository.findByName(\"HR\") == null) {\n    roleService.create(new Role(\"HR\"));\n}","handlingStrategy":"validation","validationCode":"if (roleRepository.findByName(form.name) != null) { errors.name = '角色名称已存在'; return; }","typeGuard":null,"tryCatchPattern":"catch (EntityExistException e) { // 'Role with username ... existed' actually means role NAME taken formErrors.name = '名称已存在'; }","preventionTips":["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"],"tags":["eladmin","role","duplicate","entity-exist","create"],"backgroundTag":null,"analyzedSha":"55fbf705956949697dbd68bf9003776609d3d029","analyzedAt":"2026-08-14T11:56:12.758Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}