{"record":{"id":"0c04036ed3449bdb","repo":"spring-projects/spring-security","slug":"could-not-find-required-group-x","errorCode":null,"errorMessage":"Could not find required group 'X'","messagePattern":"Could not find required group 'X'","errorType":"exception","errorClass":"EmptyResultDataAccessException","httpStatus":500,"severity":"error","filePath":"core/src/main/java/org/springframework/security/provisioning/JdbcUserDetailsManager.java","lineNumber":483,"sourceCode":"\t\t});\n\t}\n\n\t@Override\n\tpublic void addGroupAuthority(final String groupName, final GrantedAuthority authority) {\n\t\tthis.logger.debug(\"Adding authority '\" + authority + \"' to group '\" + groupName + \"'\");\n\t\tAssert.hasText(groupName, \"groupName should have text\");\n\t\tAssert.notNull(authority, \"authority cannot be null\");\n\t\tint id = findGroupId(groupName);\n\t\trequireJdbcTemplate().update(this.insertGroupAuthoritySql, (ps) -> {\n\t\t\tps.setInt(1, id);\n\t\t\tps.setString(2, authority.getAuthority());\n\t\t});\n\t}\n\n\tprivate int findGroupId(String group) {\n\t\tInteger groupId = requireJdbcTemplate().queryForObject(this.findGroupIdSql, Integer.class, group);\n\t\tif (groupId == null) {\n\t\t\tthrow new EmptyResultDataAccessException(\"Could not find required group '\" + group + \"'\", 1);\n\t\t}\n\t\treturn groupId;\n\t}\n\n\tprivate JdbcTemplate requireJdbcTemplate() {\n\t\tJdbcTemplate jdbc = getJdbcTemplate();\n\t\tAssert.notNull(jdbc, \"JdbcTemplate cannot be null\");\n\t\treturn jdbc;\n\t}\n\n\t/**\n\t * Sets the {@link SecurityContextHolderStrategy} to use. The default action is to use\n\t * the {@link SecurityContextHolderStrategy} stored in {@link SecurityContextHolder}.\n\t *\n\t * @since 5.8\n\t */\n\tpublic void setSecurityContextHolderStrategy(SecurityContextHolderStrategy securityContextHolderStrategy) {\n\t\tAssert.notNull(securityContextHolderStrategy, \"securityContextHolderStrategy cannot be null\");","sourceCodeStart":465,"sourceCodeEnd":501,"githubUrl":"https://github.com/spring-projects/spring-security/blob/96852e8860138a482cb13d1479573f24ff6443c6/core/src/main/java/org/springframework/security/provisioning/JdbcUserDetailsManager.java#L465-L501","documentation":"JdbcUserDetailsManager.findGroupId resolves a group name to its numeric id using findGroupIdSql; when the query returns null (no row found) it throws EmptyResultDataAccessException('Could not find required group X', 1). This is called from group management operations (adding users to groups, reading group authorities) whenever the referenced group does not exist in the groups table.","triggerScenarios":"Calling renameGroup, addUserToGroup, removeUserFromGroup, or findGroupAuthorities with a group name absent from the groups table; referencing groups created by a different schema/prefix; case mismatch between the group name string and the stored value.","commonSituations":"Spring Security group-authority schema (groups, group_authorities, group_members) not initialized or dropped; environments where groups were seeded manually with different names; tests reusing hardcoded group names that no DB seeding created.","solutions":["Create the group first (JdbcUserDetailsManager.createGroup or equivalent insert into groups) before referencing it","Verify the group name spelling and case against the groups table contents","Run Spring Security's default group schema DDL (create_groups.sql) if the tables are missing","Check findGroupIdSql (setFindGroupIdQuery) points at the correct table/column if using a custom schema"],"exampleFix":"// before\nmanager.addUserToGroup(username, \"ADMINS\"); // group missing -> EmptyResultDataAccessException\n// after\nif (manager.findAllGroups().stream().noneMatch(\"ADMINS\"::equals)) {\n    manager.createGroup(\"ADMINS\", AuthorityUtils.createAuthorityList(\"ROLE_ADMIN\"));\n}\nmanager.addUserToGroup(username, \"ADMINS\");","handlingStrategy":"validation","validationCode":"boolean exists = manager.findAllGroups().stream()\n    .anyMatch(g -> g.equals(groupName));\nif (!exists) {\n    throw new IllegalArgumentException(\"Group not found: \" + groupName);\n}","typeGuard":null,"tryCatchPattern":"try {\n    manager.addUserToGroup(username, groupName);\n} catch (EmptyResultDataAccessException e) {\n    if (e.getMessage() != null && e.getMessage().startsWith(\"Could not find required group\")) {\n        manager.createGroup(groupName, AuthorityUtils.NO_AUTHORITIES);\n        manager.addUserToGroup(username, groupName);\n    } else { throw e; }\n}","preventionTips":["Create groups (createGroup) before referencing them in group operations","Run Spring Security's groups schema DDL and seed data idempotently","Match group-name casing exactly as stored","Verify setFindGroupIdQuery targets the right table if using a custom schema"],"tags":["jdbc","group-not-found","empty-result","database"],"backgroundTag":"record-not-found","analyzedSha":"96852e8860138a482cb13d1479573f24ff6443c6","analyzedAt":"2026-09-10T23:25:23.477Z","contentChangedAt":"2026-09-10T23:25:23.477Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}