{"record":{"id":"84adfb578ec0cf0a","repo":"hibernate/hibernate-orm","slug":"duplicate-named-query-s","errorCode":null,"errorMessage":"Duplicate named query '%s'","messagePattern":"Duplicate named query '(.+?)'","errorType":"exception","errorClass":"DuplicateMappingException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/boot/internal/InFlightMetadataCollectorImpl.java","lineNumber":729,"sourceCode":"\t\tif ( def == null ) {\n\t\t\tthrow new IllegalArgumentException( \"Named query definition is null\" );\n\t\t}\n\t\telse if ( def.getRegistrationName() == null ) {\n\t\t\tthrow new IllegalArgumentException( \"Named query definition name is null: \" + def.getHqlString() );\n\t\t}\n\t\telse if ( !defaultNamedQueryNames.contains( def.getRegistrationName() ) ) {\n\t\t\tapplyNamedQuery( def.getRegistrationName(), def );\n\t\t}\n\t}\n\n\tprivate void applyNamedQuery(String name, NamedHqlQueryDefinition<?> query) {\n\t\tcheckQueryName( name );\n\t\tnamedQueryMap.put( name.intern(), query );\n\t}\n\n\tprivate void checkQueryName(String name) throws DuplicateMappingException {\n\t\tif ( namedQueryMap.containsKey( name ) || namedNativeQueryMap.containsKey( name ) ) {\n\t\t\tthrow new DuplicateMappingException( DuplicateMappingException.Type.QUERY, name );\n\t\t}\n\t}\n\n\t@Override\n\tpublic void addDefaultQuery(NamedHqlQueryDefinition<?> queryDefinition) {\n\t\tapplyNamedQuery( queryDefinition.getRegistrationName(), queryDefinition );\n\t\tdefaultNamedQueryNames.add( queryDefinition.getRegistrationName() );\n\t}\n\n\t// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\t// Named native-query handling\n\n\t@Override\n\tpublic NamedNativeQueryDefinition<?> getNamedNativeQueryMapping(String name) {\n\t\treturn namedNativeQueryMap.get( name );\n\t}\n\n\t@Override","sourceCodeStart":711,"sourceCodeEnd":747,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/boot/internal/InFlightMetadataCollectorImpl.java#L711-L747","documentation":"DuplicateMappingException(Type.QUERY): a named query is being registered under a name already present in either the HQL or the native-query map (checkQueryName checks both), so an @NamedQuery colliding with an @NamedNativeQuery is also a duplicate. Query names are global per SessionFactory/persistence unit. The duplicate always aborts bootstrap.","triggerScenarios":"Two @NamedQuery(name=\"X\") annotations in one persistence unit; an @NamedQuery and an @NamedNativeQuery sharing a name; hbm.xml <query name=\"X\"> duplicated by an annotation; the same orm.xml listed twice in persistence.xml; two library jars each defining the same query name.","commonSituations":"Copy-pasted entities, shared 'common model' jars reused across apps, and the Hibernate 5-to-6 upgrade where previously tolerated duplicate query names now fail fast; also mixing annotations with leftover hbm.xml mappings of the same classes.","solutions":["Search the project for the name shown in the message and rename one declaration — use a convention like EntityName.queryName","Check persistence.xml for a mapping file listed twice and remove the duplicate entry","If the duplicate lives in a third-party jar you cannot edit, isolate that mapping into its own persistence unit or exclude it from the factory","Add a CI smoke test that builds the SessionFactory so duplicates surface at build time, not at deployment"],"exampleFix":"// before — two entities, same query name\n@NamedQuery(name = \"findActive\", query = \"select u from User u where u.active = true\") // on User\n@NamedQuery(name = \"findActive\", query = \"select o from Ord o where o.open = true\") // on Ord\n\n// after\n@NamedQuery(name = \"User.findActive\", query = \"select u from User u where u.active = true\")\n@NamedQuery(name = \"Ord.findActive\", query = \"select o from Ord o where o.open = true\")","handlingStrategy":"validation","validationCode":"static void assertNoDuplicateQueryNames( Class<?>... entityClasses ) {\n    final Map<String, Class<?>> seen = new HashMap<>();\n    for ( final Class<?> c : entityClasses ) {\n        for ( final NamedQuery q : c.getAnnotationsByType( NamedQuery.class ) ) {\n            final Class<?> prev = seen.put( q.name(), c );\n            if ( prev != null ) {\n                throw new IllegalStateException( \"Duplicate @NamedQuery '\" + q.name() + \"' on \" + prev + \" and \" + c );\n            }\n        }\n        for ( final NamedNativeQuery q : c.getAnnotationsByType( NamedNativeQuery.class ) ) {\n            final Class<?> prev = seen.put( q.name(), c );\n            if ( prev != null ) {\n                throw new IllegalStateException( \"Duplicate native query name '\" + q.name() + \"' on \" + prev + \" and \" + c );\n            }\n        }\n    }\n}","typeGuard":null,"tryCatchPattern":"try {\n    sessionFactory = metadata.buildSessionFactory();\n} catch ( DuplicateMappingException e ) {\n    if ( e.getType() == DuplicateMappingException.Type.QUERY ) {\n        throw new IllegalStateException( \"Duplicate named query '\" + e.getName() + \"' — rename one declaration\", e );\n    }\n    throw e;\n}","preventionTips":["Prefix every query name with its entity (User.findActive)","Run a SessionFactory bootstrap smoke test in CI","Never define query names in shared libraries consumed by several persistence units","Check persistence.xml for duplicated <mapping-file> entries"],"tags":["hibernate","orm","jpa","java","bootstrap","named-query","duplicate-mapping"],"backgroundTag":"duplicate-named-query","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}