When using the Spring Boot 2.6.1 managed dependency org.hibernate.validator.hibernate-validator-annotation-processor in a Java 17 maven project with Records, the project can no longer be compiled with maven (e.g. with mvn clean compile or mvn clean package) as the Record classes aren't recognized as valid Java model elements:

Fatal error compiling: javax.lang.model.element.UnknownElementException:
Unknown element: "com.example.hibernatevalidatorcompileissue.api.record.Java17Record"

Full details:

$ mvn -v
Running `/Users/tofi/dev/hibernate-validator-compile-issue/mvnw`...
Apache Maven 3.8.3 (ff8e977a158738155dc465c6a97ffaf31982d739)
Maven home: /Users/tofi/.m2/wrapper/dists/apache-maven-3.8.3-bin/5a6n1u8or3307vo2u2jgmkhm0t/apache-maven-3.8.3
Java version: 17.0.1, vendor: Eclipse Adoptium, runtime: /Users/tofi/.sdkman/candidates/java/17.0.1-tem
Default locale: de_DE, platform encoding: UTF-8
OS name: "mac os x", version: "11.5", arch: "x86_64", family: "mac"

$mvn clean compile
Running `/Users/tofi/dev/hibernate-validator-compile-issue/mvnw`...
[INFO] Scanning for projects...
[INFO] 
[INFO] -----------< com.example:hibernate-validator-compile-issue >------------
[INFO] Building hibernate-validator-compile-issue 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:3.1.0:clean (default-clean) @ hibernate-validator-compile-issue ---
[INFO] Deleting /Users/tofi/dev/hibernate-validator-compile-issue/target
[INFO] 
[INFO] --- maven-resources-plugin:3.2.0:resources (default-resources) @ hibernate-validator-compile-issue ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Using 'UTF-8' encoding to copy filtered properties files.
[INFO] Copying 1 resource
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ hibernate-validator-compile-issue ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 3 source files to /Users/tofi/dev/hibernate-validator-compile-issue/target/classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  2.620 s
[INFO] Finished at: 2021-12-15T14:36:46+01:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project hibernate-validator-compile-issue: Fatal error compiling: javax.lang.model.element.UnknownElementException: Unknown element: "com.example.hibernatevalidatorcompileissue.api.record.Java17Record" -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

The full maven stack trace with -X switch prints the following root cause which lead us to the hibernate-validator-annotation-processor dependency:

...
Caused by: javax.lang.model.element.UnknownElementException: Unknown element: "com.example.hibernatevalidatorcompileissue.api.record.Java17Record"
    at javax.lang.model.util.AbstractElementVisitor6.visitUnknown (AbstractElementVisitor6.java:129)
    at javax.lang.model.util.ElementKindVisitor6.visitTypeAsRecord (ElementKindVisitor6.java:231)
    at javax.lang.model.util.ElementKindVisitor6.visitType (ElementKindVisitor6.java:160)
    at com.sun.tools.javac.code.Symbol$ClassSymbol.accept (Symbol.java:1561)
    at org.hibernate.validator.ap.ConstraintValidationProcessor.process (ConstraintValidationProcessor.java:126)
    at com.sun.tools.javac.processing.JavacProcessingEnvironment.callProcessor (JavacProcessingEnvironment.java:1023)
...

Attached you will find a minimal sample project set up on top of a Spring Initializer template:
spring-boot-hibernate-validator-compile-issue.zip

As you can see, we didn't even use the hibernate-validator or hibernate-validator-annotations in the project. It's sufficient to have the dependency on the classpath to crash the compilation. Remove it and the project compiles well!

As Spring Boot 2.6 was announced with Java 17 support I expect the Spring Boot managed dependencies to be Java 17 compatible as well.

Comment From: snicoll

Thanks for the sample.

Unfortunately, only Hibernate Validator 7.0.x is compatible with Java 17. I've added <hibernate-validator.version>7.0.2.Final</hibernate-validator.version> to your sample and it works.

As Spring Boot 2.6 was announced with Java 17 support I expect the Spring Boot managed dependencies to be Java 17 compatible as well.

I understand that but we can't upgrade to Hibernate Validator 7 as it is the version that is JakartaEE based (i.e. Spring Boot 3). Please consider creating an issue in the Hibernate Validator project to ask them to backport the fix to the 6.x generation.

Comment From: tofi86

@snicoll thanks for the quick reply, Stéphane! I kinda expected this after seeing the hibernate-validator dependency upgrade in the Spring Boot 3 branch...

Please consider creating an issue in the Hibernate Validator project to ask them to backport the fix to the 6.x generation.

Done! Reference: https://hibernate.atlassian.net/browse/HV-1863