micronaut-projects / micronaut-data

Ahead of Time Data Repositories
Apache License 2.0
459 stars 196 forks source link

Java 22 issue: Cannot query entity [XXX] on non-existent property: Id #2971

Closed sergey-morenets closed 3 weeks ago

sergey-morenets commented 3 weeks ago

Expected Behavior

Micronaut project should support Java 22.

Actual Behaviour

Hi

We changed our project JDK from 21 to 22 in JAVA_HOME. After that our Micronaut project fails to compile with error:

Unable to implement Repository method: PaymentProviderRepository.findById(Object id). Cannot query entity [PaymentProvider] on non-existent property: Id.

If we downgrade JDK from 22 to 21 then compilation succeeds. We use Lombok 1.18.32 (latest version) in our project. If we remove @Getter/@Setter annotations on BaseEntity and create getters/setters explicitly then compilation succeeds with JDK 22.

Steps To Reproduce

Here's our code and configuration:

@Getter
@Setter
@ReflectiveAccess
@MappedEntity
public class PaymentProvider extends BaseEntity {

    private String name;

    private boolean enabled = true;

}
@Getter
@Setter
public abstract class BaseEntity {

    @Id
    @GeneratedValue
    private ObjectId id;

    private String createdBy;

    @DateCreated
    private LocalDateTime createdAt;

    @DateUpdated
    private LocalDateTime lastModifiedAt;
}
@MongoRepository
@Transactional(propagation = Propagation.NOT_SUPPORTED)
public interface PaymentProviderRepository extends CrudRepository<PaymentProvider, ObjectId> {  
}
<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <annotationProcessorPaths>
                        <path>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                            <version>${lombok.version}</version>
                        </path>
                        <path>
                            <groupId>io.micronaut</groupId>
                            <artifactId>micronaut-inject-java</artifactId>
                            <version>${micronaut.core.version}</version>
                        </path>
                        <path>
                            <groupId>io.micronaut</groupId>
                            <artifactId>micronaut-http-validation</artifactId>
                            <version>${micronaut.core.version}</version>
                        </path>
                        <path>
                            <groupId>io.micronaut.serde</groupId>
                            <artifactId>micronaut-serde-processor</artifactId>
                            <version>${micronaut.serialization.version}</version>
                            <exclusions>
                                <exclusion>
                                    <groupId>io.micronaut</groupId>
                                    <artifactId>micronaut-inject</artifactId>
                                </exclusion>
                            </exclusions>
                        </path>
                        <path>
                            <groupId>io.micronaut.validation</groupId>
                            <artifactId>micronaut-validation-processor</artifactId>
                            <version>${micronaut.validation.version}</version>
                            <exclusions>
                                <exclusion>
                                    <groupId>io.micronaut</groupId>
                                    <artifactId>micronaut-inject</artifactId>
                                </exclusion>
                            </exclusions>
                        </path>
                        <path>
                            <groupId>io.micronaut.data</groupId>
                            <artifactId>micronaut-data-document-processor</artifactId>
                            <version>${micronaut.data.version}</version>
                        </path>
                    </annotationProcessorPaths>
                    <useIncrementalCompilation>false</useIncrementalCompilation>
                </configuration>
            </plugin>

Environment Information

JDK version: 22.0.1

Example Application

No response

Version

4.4.3

dstepanov commented 3 weeks ago

Please report it to the Lombok project. There is nothing we can do here.

sergey-morenets commented 3 weeks ago

@dstepanov Why do you think that it's Lombok issue? We have multiple Spring Boot projects with Java 22/Lombok and they compile and run without any issues. Lombok perfectly supports Java 22. So it's clearly Micronaut Data problem that it doesn't support Java 22. Please, mention that fact as limitation in the documentation.

dstepanov commented 3 weeks ago

You wrote that it works without Lombok, so why would it be a Micronaut limitation?

Lombok has to modify the Java compiler's private and internal models. It does not seem to be able to do this properly for Java 22, which breaks the annotation processors.

sergey-morenets commented 3 weeks ago

Please, read carefully my comments below. Lombok 1.18.32 is fully compatible with Java 22 and is able to generate getters and setters if you put @Getter/@Setter annotation on POJO classes. I checked that again today. But due to unknown reason Micronaut Data raises an error in such case (Lombok + Java 22). You can't just ignore this error because it would happen for any Micronaut project that will migrate from Java 21 to Java 22. So if you don't want to investigate this error, please add this fact as limitation in the documentation.

dstepanov commented 3 weeks ago

Please understand how Lombok works. It uses Javac's private/internal API to alter the internal model, add more methods, etc. For some unknown reason, those changes are not visible using the public annotation processor API. Just because the methods are generated doesn't mean it works correctly.

There is nothing that can be done to support it or fix it on our side. The BUG is on the Lombok side, please report it there.