hibernate/hibernate-orm · error · MappingException

No filter condition found for filter [%s] associated with ma

Error message

No filter condition found for filter [%s] associated with many-to-many [%s]

What it means

When binding filters attached to a many-to-many element, each named filter must resolve to a SQL condition - either the condition= attribute on the <filter> element itself or the default condition declared in the matching <filter-def>. If the filter has a name but no resolvable condition, Hibernate cannot build the SQL restriction and fails before the SessionFactory is created.

Source

Thrown at hibernate-core/src/main/java/org/hibernate/boot/model/source/internal/hbm/ModelBinder.java:3243

				bindManyToManyFilter( mappingDocument, collectionBinding, filterSource );
			}
		}

		private void bindManyToManyFilter(
				MappingDocument mappingDocument, Collection collectionBinding, FilterSource filterSource) {
			final String name = filterSource.getName();
			if ( name == null ) {
				if ( BOOT_LOGGER.isTraceEnabled() ) {
					BOOT_LOGGER.tracef(
							"Encountered filter with no name associated with many-to-many [%s]; skipping",
							getPluralAttributeSource().getAttributeRole().getFullPath()
					);
				}
			}
			else {
				final String condition = filterSource.getCondition();
				if ( condition == null ) {
					throw new MappingException(
							"No filter condition found for filter [%s] associated with many-to-many [%s]"
									.formatted( name, getPluralAttributeSource().getAttributeRole().getFullPath() ),
							mappingDocument.getOrigin()
					);
				}
				if ( BOOT_LOGGER.isTraceEnabled() ) {
					BOOT_LOGGER.tracef(
							"Applying many-to-many filter [%s] as [%s] to collection [%s]",
							name,
							condition,
							getPluralAttributeSource().getAttributeRole().getFullPath()
					);
				}
				collectionBinding.addManyToManyFilter(
						name,
						condition,
						filterSource.shouldAutoInjectAliases(),
						filterSource.getAliasToTableMap(),

View on GitHub (pinned to fad1729dce)

Solutions

  1. Add condition='...' to the <filter> element itself
  2. Or declare (or fix) the matching <filter-def> with a default condition and ensure its file is loaded
  3. Check the filter name spelling against the filter-def name

Example fix

// before (no filter-def anywhere)
<many-to-many class='Role'>
    <filter name='active'/>
</many-to-many>

// after
<filter-def name='active' condition='deleted = false'/>
...
<many-to-many class='Role'>
    <filter name='active' condition='deleted = false'/>
</many-to-many>
Defensive patterns

Strategy: validation

Validate before calling

// every <filter name='x'> must find a condition locally or in a <filter-def name='x'>
Map<String, String> defs = new HashMap<>();
NodeList fd = doc.getElementsByTagName("filter-def");
for (int i = 0; i < fd.getLength(); i++) { Element d = (Element) fd.item(i); defs.put(d.getAttribute("name"), d.getAttribute("condition")); }
NodeList filters = doc.getElementsByTagName("filter");
for (int i = 0; i < filters.getLength(); i++) {
    Element f = (Element) filters.item(i);
    String name = f.getAttribute("name");
    boolean local = f.getAttributeNode("condition") != null && !f.getAttribute("condition").isBlank();
    if (!local && !defs.containsKey(name)) {
        throw new IllegalStateException("filter '" + name + "' has no condition and no filter-def");
    }
}

Try / catch

catch (MappingException e) at bootstrap; the message names the filter and the many-to-many collection. Add condition= to that <filter> or load a <filter-def> providing a default condition.

Prevention

When it happens

Trigger: <filter name='active'/> inside a <many-to-many> element when no <filter-def name='active' condition='...'> exists anywhere in the loaded mappings and the <filter> element itself has no condition attribute.

Common situations: The <filter-def> lives in another mapping file that is not added to the Configuration; the filter name is misspelled so no filter-def matches; assuming a condition-less filter is a no-op.

Related errors


AI-assisted analysis of hibernate/hibernate-orm@fad1729dce (2026-08-22). Data as JSON: /api/errors/1f85aa4759d5f281. Report an issue: GitHub.