hibernate/hibernate-orm · error · AnnotationException
'@AttributeAccessor' annotation must specify a 'strategy'
Error message
'@AttributeAccessor' annotation must specify a 'strategy'
What it means
@AttributeAccessor's strategy attribute defaults to the PropertyAccessStrategy interface itself, which acts as the 'not specified' marker. AttributeAccessorBinder treats that default as a mapping error - an accessor annotation without a strategy would silently change nothing - and throws AnnotationException at bootstrap.
Source
Thrown at hibernate-core/src/main/java/org/hibernate/binder/internal/AttributeAccessorBinder.java:32
/**
* Configures the {@link PropertyAccessStrategy} for an attribute.
*
* @author Gavin King
*/
public class AttributeAccessorBinder implements AttributeBinder<AttributeAccessor> {
@Override
public void bind(
AttributeAccessor accessor,
MetadataBuildingContext buildingContext,
PersistentClass persistentClass,
Property property) {
final var type = accessor.strategy();
if ( !PropertyAccessStrategy.class.equals(type) ) {
property.setPropertyAccessorName( type.getName() );
}
else {
throw new AnnotationException("'@AttributeAccessor' annotation must specify a 'strategy'");
}
}
}
View on GitHub (pinned to fad1729dce)
Solutions
- Specify a concrete strategy: @AttributeAccessor(strategy = MyPropertyAccessStrategy.class).
- If no custom accessor is wanted, delete the annotation.
Example fix
// before @AttributeAccessor // or strategy = PropertyAccessStrategy.class private String name; // after @AttributeAccessor(strategy = MyPropertyAccessStrategy.class) private String name;
Defensive patterns
Strategy: validation
Validate before calling
// scan entities at startup so the bad annotation surfaces before runtime
for (Class<?> c : entityClasses) {
for (Field f : c.getDeclaredFields()) {
AttributeAccessor a = f.getAnnotation(AttributeAccessor.class);
if (a != null && PropertyAccessStrategy.class.equals(a.strategy())) {
throw new IllegalStateException(c.getName() + "." + f.getName() + " has @AttributeAccessor without a strategy");
}
}
} Prevention
- Always specify strategy = YourPropertyAccessStrategy.class on @AttributeAccessor
- Delete the annotation when no custom access strategy is needed
When it happens
Trigger: Writing @AttributeAccessor with no attributes (or explicitly strategy = PropertyAccessStrategy.class) on an entity field/property/class during annotation binding.
Common situations: IDE auto-completion inserting the marker interface; copy-pasting the annotation without the strategy attribute; intending a custom accessor but forgetting the attribute.
Related errors
- Write expression in '@ColumnTransformer' for property '${pro
- Cannot perform #forceNotNull because internal org.hibernate.
- '@ColumnDefault' may only be applied to single-column mappin
- '@GeneratedColumn' may only be applied to single-column mapp
- '@Check' may only be applied to single-column mappings but '
AI-assisted analysis of hibernate/hibernate-orm@fad1729dce (2026-08-22).
Data as JSON: /api/errors/c038a253b9ce92bd.
Report an issue: GitHub.