hibernate/hibernate-orm · error · IllegalArgumentException

Unable to locate %s with the given name [%s] on this Managed

Error message

Unable to locate %s with the given name [%s] on this ManagedType [%s]

What it means

A metamodel lookup by name failed: getDeclaredAttribute(name) (and the collection get* methods via checkNotNull) found no declared attribute with that exact name on this ManagedType. IllegalArgumentException names the requested attribute type, the name, and the type. Names are Java property names, not column names, and the declared variant does not see inherited attributes.

Source

Thrown at hibernate-core/src/main/java/org/hibernate/metamodel/model/domain/internal/AbstractManagedType.java:247

		else if ( declaredPluralAttributes != null ) {
			return declaredPluralAttributes.get( name );
		}
		else {
			return null;
		}
	}

	@Override
	@Nonnull
	public PersistentAttribute<J,?> getDeclaredAttribute(@Nonnull String name) {
		final var attribute = findDeclaredAttribute( name );
		checkNotNull( "Attribute", attribute, name );
		return attribute;
	}

	private void checkNotNull(String attributeType, Attribute<?,?> attribute, String name) {
		if ( attribute == null ) {
			throw new IllegalArgumentException(
					String.format(
							"Unable to locate %s with the given name [%s] on this ManagedType [%s]",
							attributeType,
							name,
							getTypeName()
					)
			);
		}
	}

	@Override
	public String getTypeName() {
		return hibernateTypeName;
	}


	// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	// Singular attributes

View on GitHub (pinned to fad1729dce)

Solutions

  1. Use the exact Java property name and the non-declared getAttribute(name) to also find inherited attributes
  2. List valid names when debugging: managedType.getAttributes().forEach(a -> System.out.println(a.getName()))
  3. Prefer the canonical JPA metamodel class (StaticMetamodel) or the SingularAttribute references to get compile-time safety instead of strings

Example fix

// before
Attribute<Person,?> a = personType.getDeclaredAttribute("EMAIL"); // column name -> throws

// after
Attribute<Person,?> a = personType.getAttribute("email"); // Java property name, inherited ok
Defensive patterns

Strategy: type-guard

Validate before calling

// Validate the name before the typed getter
String name = /* input */;
boolean exists = StreamSupport.stream(managedType.getAttributes().spliterator(), false)
        .anyMatch(a -> a.getName().equals(name));
if (!exists) throw new IllegalArgumentException("Unknown attribute '" + name + "' on " + managedType.getTypeName());

Type guard

static java.util.Optional<Attribute<?,?>> findAttr(ManagedType<?> type, String name) {
    for (Attribute<?,?> a : type.getAttributes()) {
        if (a.getName().equals(name)) return java.util.Optional.of(a);
    }
    return java.util.Optional.empty(); // also searches supertype via getAttributes()
}

Try / catch

try {
    return managedType.getDeclaredAttribute(name);
} catch (IllegalArgumentException e) {
    if (e.getMessage() != null && e.getMessage().contains("Unable to locate")) {
        throw new NoSuchFieldException("No attribute '" + name + "' on " + managedType.getTypeName());
    }
    throw e;
}

Prevention

When it happens

Trigger: managedType.getDeclaredAttribute("email") when the field is 'eMail' or inherited from a superclass; using the DB column name ("EMAIL" or "email_address") instead of the property name; typos in Criteria/JPQL metamodel usage; calling the declared variant for inherited fields.

Common situations: Renaming a field without updating all metamodel/string lookups. Mixing column naming strategies (camelCase vs snake_case) — attributes use the Java name. Code generation with wrong casing. Inherited attributes (declared variant misses them).

Related errors


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