{"record":{"id":"e089edb007627405","repo":"quarkusio/quarkus","slug":"panacheentity-s-cannot-be-defined-for-usage-in","errorCode":null,"errorMessage":"PanacheEntity '%s' cannot be defined for usage in several persistence units which is not supported. The following persistence units were found: %s.","messagePattern":"PanacheEntity '(.+?)' cannot be defined for usage in several persistence units which is not supported\\. The following persistence units were found: (.+?)\\.","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"extensions/panache/hibernate-orm-panache-kotlin/deployment/src/main/java/io/quarkus/hibernate/orm/panache/kotlin/deployment/KotlinPanacheResourceProcessor.java","lineNumber":163,"sourceCode":"\n        Map<String, Set<String>> collectedEntityToPersistenceUnits;\n        boolean incomplete;\n        if (jpaModelPersistenceUnitMapping.isPresent()) {\n            collectedEntityToPersistenceUnits = jpaModelPersistenceUnitMapping.get().getEntityToPersistenceUnits();\n            incomplete = jpaModelPersistenceUnitMapping.get().isIncomplete();\n        } else {\n            collectedEntityToPersistenceUnits = new HashMap<>();\n            // This happens if there is no persistence unit, in which case we definitely know this metadata is complete.\n            incomplete = false;\n        }\n\n        Map<String, String> panacheEntityToPersistenceUnit = new HashMap<>();\n        for (Map.Entry<String, Set<String>> entry : collectedEntityToPersistenceUnits.entrySet()) {\n            String entityName = entry.getKey();\n            List<String> selectedPersistenceUnits = new ArrayList<>(entry.getValue());\n            boolean isPanacheEntity = modelClasses.contains(entityName);\n            if (selectedPersistenceUnits.size() > 1 && isPanacheEntity) {\n                throw new IllegalStateException(String.format(\n                        \"PanacheEntity '%s' cannot be defined for usage in several persistence units which is not supported. The following persistence units were found: %s.\",\n                        entityName, String.join(\",\", selectedPersistenceUnits)));\n            }\n\n            panacheEntityToPersistenceUnit.put(entityName, selectedPersistenceUnits.get(0));\n        }\n        // This is called even if there are no entity types, so that Panache gets properly initialized.\n        recorder.addEntityTypesToPersistenceUnit(panacheEntityToPersistenceUnit, incomplete);\n    }\n\n    private void processRepositories(CombinedIndexBuildItem index,\n            BuildProducer<BytecodeTransformerBuildItem> transformers,\n            List<String> classNamesToRegisterForReflection,\n            PanacheRepositoryEnhancer enhancer,\n            ByteCodeType baseType,\n            ByteCodeType type) {\n\n        Set<Type> typeParameters = new HashSet<>();","sourceCodeStart":145,"sourceCodeEnd":181,"githubUrl":"https://github.com/quarkusio/quarkus/blob/e1c734241f34c7919086ceb4c9262b4a58f6de44/extensions/panache/hibernate-orm-panache-kotlin/deployment/src/main/java/io/quarkus/hibernate/orm/panache/kotlin/deployment/KotlinPanacheResourceProcessor.java#L145-L181","documentation":"At deployment, Kotlin Panache collects which persistence units each PanacheEntity is used with. PanacheEntity subclasses carry a hard-wired persistence unit reference, so an entity assigned to more than one persistence unit is unsupported and the build fails with IllegalStateException listing the conflicting units.","triggerScenarios":"A class extending PanacheEntity (or KotlinPanacheEntity) being referenced/annotated for multiple persistence units in quarkus.hibernate-orm.* config (e.g. an entity package or class mapped by two named persistence units).","commonSituations":"Multi-tenancy or multi-database setups where packages are included in several persistence unit configurations; copy-pasted persistence unit configs overlapping in their entity packages.","solutions":["Make the entity extend PanacheEntityBase instead and manage persistence with an injected EntityManager, or use the active-entity pattern per unit.","Configure disjoint packages/classes per persistence unit so each PanacheEntity belongs to exactly one (quarkus.hibernate-orm.<name>.packages).","Split the shared entity into one subclass per persistence unit.","Review the listed persistence unit names in the message and remove the duplicate mapping."],"exampleFix":"// before\nclass Customer : PanacheEntity() // mapped by both 'default' and 'audit' PUs -> build fails\n\n// after\n@ApplicationScoped\nclass AuditCustomerRepository { // per-PU repository instead of shared PanacheEntity\n    fun find(id: Long): Customer? =\n        withEntityManager(\"audit\") { it.find(Customer::class.java, id) }\n}","handlingStrategy":"validation","validationCode":"// build-time check: ensure each PanacheEntity package is mapped by exactly one PU\nSet<String> pus = persistenceUnitsFor(entityClass);\nif (pus.size() > 1) {\n    throw new IllegalStateException(\"Entity \" + entityClass + \" mapped by multiple PUs: \" + pus);\n}","typeGuard":null,"tryCatchPattern":"// Deployment-time failure; cannot be caught at runtime.\n// Fix configuration; optionally assert in build scripts:\n// fail build if entityPackages sets of all quarkus.hibernate-orm.<pu>.packages intersect","preventionTips":["Give each persistence unit disjoint packages in quarkus.hibernate-orm.<name>.packages.","Extend PanacheEntityBase and use repositories when an entity must serve multiple units.","Audit persistence unit config after adding a new named PU.","Run the build (not just tests) to surface deployment-time violations early."],"tags":["panache","kotlin","persistence-unit","deployment","multi-tenancy"],"backgroundTag":"entity-multiple-persistence-units","analyzedSha":"e1c734241f34c7919086ceb4c9262b4a58f6de44","analyzedAt":"2026-09-05T17:01:29.979Z","contentChangedAt":"2026-09-05T17:01:29.979Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}