{"record":{"id":"dd5f973d03578c25","repo":"hibernate/hibernate-orm","slug":"no-name-provided-and-multiple-persistence-units-fo","errorCode":null,"errorMessage":"No name provided and multiple persistence units found","messagePattern":"No name provided and multiple persistence units found","errorType":"exception","errorClass":"PersistenceException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/jpa/HibernatePersistenceProvider.java","lineNumber":98,"sourceCode":"\t\t\t\tpersistenceUnitName,\n\t\t\t\tproperties,\n\t\t\t\tnull,\n\t\t\t\tnull\n\t\t);\n\t}\n\n\tprivate EntityManagerFactoryBuilder getEntityManagerFactoryBuilderOrNull(\n\t\t\tString persistenceUnitName,\n\t\t\tMap<?,?> properties,\n\t\t\t@Nullable ClassLoader providedClassLoader,\n\t\t\t@Nullable ClassLoaderService providedClassLoaderService) {\n\t\tJPA_LOGGER.attemptingToObtainEmfBuilder( persistenceUnitName );\n\t\tfinal var integration = wrap( properties );\n\t\tfinal var units = locatePersistenceUnits( integration, providedClassLoader, providedClassLoaderService );\n\t\tJPA_LOGGER.locatedAndParsedPersistenceUnits( units.size() );\n\t\tif ( persistenceUnitName == null && units.size() > 1 ) {\n\t\t\t// no persistence-unit name was specified, and we found multiple persistence-units\n\t\t\tthrow new PersistenceException( \"No name provided and multiple persistence units found\" );\n\t\t}\n\n\t\tfor ( var persistenceUnit : units ) {\n\t\t\tif ( JPA_LOGGER.isTraceEnabled() ) {\n\t\t\t\tJPA_LOGGER.checkingPersistenceUnitName(\n\t\t\t\t\t\tpersistenceUnit.getName(),\n\t\t\t\t\t\tpersistenceUnit.getProviderClassName(),\n\t\t\t\t\t\tpersistenceUnitName\n\t\t\t\t);\n\t\t\t}\n\n\t\t\tfinal boolean matches =\n\t\t\t\t\tpersistenceUnitName == null\n\t\t\t\t\t|| persistenceUnit.getName().equals( persistenceUnitName );\n\t\t\tif ( matches ) {\n\t\t\t\t// See if we (Hibernate) are the persistence provider\n\t\t\t\tif ( isProvider( persistenceUnit, properties ) ) {\n\t\t\t\t\treturn providedClassLoaderService == null","sourceCodeStart":80,"sourceCodeEnd":116,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/jpa/HibernatePersistenceProvider.java#L80-L116","documentation":"HibernatePersistenceProvider.getEntityManagerFactoryBuilderOrNull scans META-INF/persistence.xml resources and, when the caller supplied no persistence-unit name and more than one unit was found, refuses to guess and throws this PersistenceException. Exactly one candidate (or an explicit name) is required to bootstrap an EntityManagerFactory.","triggerScenarios":"Persistence.createEntityManagerFactory called with a null/empty unit name, or a Spring LocalContainerEntityManagerFactoryBean with no persistenceUnitName, while the classpath contains multiple persistence.xml files or a single file with several <persistence-unit> entries.","commonSituations":"Adding a dependency JAR that ships its own META-INF/persistence.xml; multi-module deployments merged into one classpath; fat/uber-jars aggregating several JPA modules; test classpaths pulling in another module's persistence.xml.","solutions":["Pass the explicit unit name: Persistence.createEntityManagerFactory(\"main\") or setPersistenceUnitName(\"main\") / @PersistenceContext(unitName = \"main\")","Locate the stray persistence.xml (search the dependency tree / packaged jar) and remove, relocate, or rename the unit","If several units are intentional, always configure consumers with an explicit unit name"],"exampleFix":"// before\nEntityManagerFactory emf = Persistence.createEntityManagerFactory(null); // ambiguous\n\n// after\nEntityManagerFactory emf = Persistence.createEntityManagerFactory(\"main\"); // matches <persistence-unit name=\"main\">","handlingStrategy":"validation","validationCode":"ClassLoader cl = Thread.currentThread().getContextClassLoader();\nint units = 0;\nfor (java.net.URL url : java.util.Collections.list(cl.getResources(\"META-INF/persistence.xml\"))) {\n    units++;\n}\nif (units > 1 && unitName == null) {\n    throw new IllegalStateException(\"Persistence unit name required; \" + units + \" persistence.xml files on classpath\");\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always pass an explicit persistence-unit name in multi-module apps","Scan the dependency tree for jars that bundle META-INF/persistence.xml","Fail fast at startup with a clear message instead of relying on the provider's exception"],"tags":["jpa","bootstrap","persistence-unit","classpath"],"backgroundTag":"multiple-persistence-units","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}