{"record":{"id":"64d39eb3d1a838a5","repo":"hibernate/hibernate-orm","slug":"avoid-this-operation-does-not-perform-well","errorCode":null,"errorMessage":"Avoid this operation: does not perform well","messagePattern":"Avoid this operation: does not perform well","errorType":"exception","errorClass":"UnsupportedOperationException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/internal/util/collections/IdentityMap.java","lineNumber":95,"sourceCode":"\n\t@Override\n\tpublic int size() {\n\t\treturn map.size();\n\t}\n\n\t@Override\n\tpublic boolean isEmpty() {\n\t\treturn map.isEmpty();\n\t}\n\n\t@Override\n\tpublic boolean containsKey(Object key) {\n\t\treturn map.containsKey( new IdentityKey<>( key ) );\n\t}\n\n\t@Override\n\tpublic boolean containsValue(Object val) {\n\t\tthrow new UnsupportedOperationException( \"Avoid this operation: does not perform well\" );\n\t\t//return map.containsValue( val );\n\t}\n\n\t@Override\n\tpublic V get(Object key) {\n\t\treturn map.get( new IdentityKey<>( key ) );\n\t}\n\n\t@Override\n\tpublic V put(K key, V value) {\n\t\tthis.entryArray = null;\n\t\treturn map.put( new IdentityKey<>( key ), value );\n\t}\n\n\t@Override\n\tpublic V remove(Object key) {\n\t\tthis.entryArray = null;\n\t\treturn map.remove( new IdentityKey<>( key ) );","sourceCodeStart":77,"sourceCodeEnd":113,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/internal/util/collections/IdentityMap.java#L77-L113","documentation":"IdentityMap keys entries by object identity (==) wrapped in IdentityKey. A containsValue(value) call has no index to use — it would have to scan every entry — so Hibernate explicitly disables it with UnsupportedOperationException instead of quietly offering a deceptively slow operation. This is a deliberate API design choice, not a transient failure.","triggerScenarios":"Calling containsValue(...) on an IdentityMap (or on a Map view obtained from Hibernate internals that is implemented by IdentityMap); generic Map-processing utility code that calls the full Map interface including containsValue.","commonSituations":"Custom diagnostics or instrumentation that walks persistence-context-related maps as plain java.util.Map and assumes every method is supported; generic helper methods like 'mapHasValue(Map m, Object v)' applied to an IdentityMap.","solutions":["Do not call containsValue on identity-keyed Hibernate maps; ask the question differently (key-based lookup)","If you truly need it, iterate values() yourself so the cost is explicit","Filter IdentityMap instances out before generic Map processing"],"exampleFix":"// before\nboolean present = identityMap.containsValue( value );\n// after: explicit scan, cost visible at the call site\nboolean present = false;\nfor ( V v : identityMap.values() ) {\n    if ( Objects.equals( value, v ) ) { present = true; break; }\n}","handlingStrategy":"type-guard","validationCode":"// Ask the question without containsValue\nstatic <K, V> boolean containsValueSlow(Map<K, V> map, V value) {\n    for ( V v : map.values() ) {\n        if ( Objects.equals( value, v ) ) return true;\n    }\n    return false;\n}","typeGuard":"static boolean supportsContainsValue(Map<?, ?> map) {\n    return !( map instanceof org.hibernate.internal.util.collections.IdentityMap );\n}","tryCatchPattern":"try {\n    map.containsValue( v );\n} catch ( UnsupportedOperationException e ) {\n    // identity-keyed map: fall back to an explicit values() scan\n}","preventionTips":["Assume Hibernate-internal Maps may not implement the full Map contract","Prefer key-based lookups over value scans on identity maps","Wrap received maps in your own adapter that implements containsValue via values() if generic code needs it"],"tags":["hibernate","unsupported-operation","identity-map","collections"],"backgroundTag":"unsupported-operation","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}