Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Node.js, PHP and APIs

Hibernate Validator rejects constraints on a method that implements an interface

Your error

jakarta.validation.ConstraintDeclarationException1 more line it can show

Fix it fast

Most common cause, 1 of 3

The implementing method adds @Valid or constraints the interface does not have

Move the annotations up to the interface method and leave the implementation's parameters free of them.

Fix
public interface PetApi {
    Pet addPet(@Valid @NotNull Pet pet);
}

@Service
@Validated
public class PetService implements PetApi {
    @Override
    public Pet addPet(Pet pet) { /* ... */ }
}
Did this fix it?
Full steps for this cause: check, fix, verify

Short answer

HV000151 means a method that overrides or implements another one declares parameter constraints or @Valid that are not exactly those of the supertype method. Keep the annotations only on the interface, strip them from the implementing class, or set hibernate.validator.allow_parameter_constraint_override when the interface is generated. Applies to Hibernate Validator 9.1 and Spring Boot 4.

  • Applies to Hibernate Validator 9.1.6.Final, Jakarta Validation 3.1, Spring Boot 4.2
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Which cause do you have?

The message names two methods: the implementing one first, the interface or superclass one second. Print the parameter annotations of both and compare them side by side.

Run this first
grep -n -A3 "addPet(" src/main/java/com/example/api/PetApi.java
grep -n -A3 "addPet(" src/main/java/com/example/web/PetController.java

Then find the line you see in this list and open its fix:

  1. 1The implementing method adds @Valid or constraints the interface does not haveThe interface method has no constraint annotations on its parameters, while the controller or service method adds @Valid, @NotNull or similar
  2. 2The implementation copies the interface annotations, but not all of themBoth methods carry annotations on the same parameter, but the sets differ, for example @Valid on both and @NotNull only on the interface
  3. 3The interface comes from a generator or a library you cannot editThe interface method named second in the message lives in generated sources or in a dependency jar, and the implementation needs its own constraints

What does the error look like?

  • jakarta.validation.ConstraintDeclarationException
  • HV000151: A method overriding another method must not redefine the parameter constraint configuration, but method <implementing method> redefines the configuration of <interface method>.

Cause 1 of 3

The implementing method adds @Valid or constraints the interface does not have

You seeThe interface method has no constraint annotations on its parameters, while the controller or service method adds @Valid, @NotNull or similar

Check

Hibernate Validator throws HV000151 when the overriding method is parameter-constrained and its configuration differs from the supertype method. A parameter marked @Valid counts as constrained just like one with @NotNull. The reference guide states that parameter constraints on an overriding method are not allowed even when the supertype declares none.

Check
grep -rn "@Valid\|@NotNull\|@Size" src/main/java/com/example/web/PetController.java

Fix

Move the annotations up to the interface method and leave the implementation's parameters free of them. Callers through the interface then see the same rules the implementation relies on.

Fix
public interface PetApi {
    Pet addPet(@Valid @NotNull Pet pet);
}

@Service
@Validated
public class PetService implements PetApi {
    @Override
    public Pet addPet(Pet pet) { /* ... */ }
}

Verify

The application starts, and calling the method with an invalid argument now ends in a constraint violation rather than HV000151.

Verify
mvn test

Confirmed by docs.jboss.org

Did this fix it?

Cause 2 of 3

The implementation copies the interface annotations, but not all of them

You seeBoth methods carry annotations on the same parameter, but the sets differ, for example @Valid on both and @NotNull only on the interface

Check

The rule compares the full parameter configuration of both methods: the constraints on each parameter, any cross-parameter constraint and the @Valid cascade. Any difference, even a missing @NotNull, counts as redefining the configuration. An exact copy passes the check, yet it only repeats what the interface already says.

Check
grep -n -B2 -A2 "Pet pet" src/main/java/com/example/api/PetApi.java src/main/java/com/example/web/PetController.java

Fix

Delete the validation annotations from the implementing method's parameters and keep them in one place, the interface. Framework annotations that are not constraints can stay.

Fix
@Override
public Pet addPet(Pet pet) {
    return repository.save(pet);
}

Verify

The application context loads and validation still rejects a null or invalid Pet.

Verify
mvn test

Confirmed by raw.githubusercontent.com

Did this fix it?

Cause 3 of 3

The interface comes from a generator or a library you cannot edit

You seeThe interface method named second in the message lives in generated sources or in a dependency jar, and the implementation needs its own constraints

Check

Section 12.3 of the reference guide lets Hibernate Validator relax the inheritance preconditions. The property hibernate.validator.allow_parameter_constraint_override is false by default, and the guide shows provider options being passed through Configuration#addProperty(). The guide also warns that the result no longer follows the specification.

Check
grep -rn "interface PetApi" target/generated-sources src/main/java

Fix

In Spring Boot 4, register a ValidationConfigurationCustomizer bean that adds the property to the Bean Validation configuration. Treat it as a deliberate exception, since subclasses may then demand more than the interface promises.

Fix
import org.springframework.boot.validation.autoconfigure.ValidationConfigurationCustomizer;

@Bean
ValidationConfigurationCustomizer allowParameterConstraintOverride() {
    return configuration -> configuration.addProperty(
            "hibernate.validator.allow_parameter_constraint_override", "true");
}

Verify

The application starts with the constraints on the implementation, and an invalid call fails with a constraint violation.

Verify
mvn spring-boot:run

Confirmed by docs.spring.io

Did this fix it?

How do I stop it coming back?

  • Decide per API whether constraints live on the interface or on the class, and write it in the project README.
  • Configure code generators to emit bean validation on the interface, so nobody adds it by hand on the controller.
  • Give every validated service or controller method a test that calls it, because HV000151 is raised at runtime while Hibernate Validator builds metadata for the class, never by the compiler.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine on the same stack: Dispatch Pro.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.