Rabu, 14 Januari 2026

How to Resolve Hibernate Metamodel (jpamodelgen → hibernate-processor) Not Automatically Generated in Spring Boot 4.0.1

For Java developers using Spring Data JPA, the JPA Metamodel (classes usually ending with _, e.g., User_.java) is essential for creating type-safe queries using the Criteria API.

Typically, simply adding the hibernate-processor is enough to make everything work. However, if you are currently facing an issue where the metamodel is still not generated even after adding the dependency, please follow the steps below to resolve it.

1. Verify Dependency Completeness

First, ensure that the hibernate-processor dependency is installed. Additionally, for modern Java environments in Spring Boot 4, adding jakarta.xml.bind-api is often required to ensure the annotation processing runs perfectly.

Add the following lines to your pom.xml:

XML
<dependency>
    <groupId>org.hibernate.orm</groupId>
    <artifactId>hibernate-processor</artifactId>
</dependency>
<dependency>
    <groupId>jakarta.xml.bind</groupId>
    <artifactId>jakarta.xml.bind-api</artifactId>
</dependency>

2. Check Build Plugin Configuration (Important)

This is a common cause that often goes unnoticed. If the metamodel still doesn't appear after the first step, it is highly likely that a maven-compiler-plugin configuration is "blocking" the Hibernate Processor.

If your pom.xml contains a configuration block like the one below (usually automatically added when using Lombok), it is recommended to remove it:

XML
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <annotationProcessorPaths>
                    <path>
                        <groupId>org.projectlombok</groupId>
                        <artifactId>lombok</artifactId>
                    </path>
                </annotationProcessorPaths>
            </configuration>
        </plugin>
    </plugins>
</build>

Removing this explicit configuration allows Maven to automatically detect all processors (both Lombok and Hibernate) directly from the registered dependencies.

3. Complete POM Implementation Example

Below is an example of a clean pom.xml that has been proven to successfully generate the metamodel:

XML
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-parent</artifactId>
       <version>4.0.1</version>
       <relativePath/> </parent>
    
    <groupId>com.example.app</groupId>
    <artifactId>demo-project</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo-project</name>
    <description>Demo Application</description>
    
    <properties>
       <java.version>25</java.version>
       <maven.compiler.source>25</maven.compiler.source>
       <maven.compiler.target>25</maven.compiler.target>
       <maven.compiler.proc>full</maven.compiler.proc>
    </properties>
    
    <dependencies>
        <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-test</artifactId>
          <scope>test</scope>
        </dependency>
        
        <dependency>
            <groupId>org.hibernate.orm</groupId>
            <artifactId>hibernate-processor</artifactId>
        </dependency>
        <dependency>
            <groupId>jakarta.xml.bind</groupId>
            <artifactId>jakarta.xml.bind-api</artifactId>
        </dependency>
        </dependencies>
 
<!-- OPTIONAL -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                     <mainClass>com.example.app.DemoApplication</mainClass>
                </configuration>
                 <executions>
                     <execution>
                         <goals>
                             <goal>repackage</goal>
                         </goals>
                     </execution>
                 </executions>
            </plugin>
        </plugins>
    </build>
<!-- END-OPTIONAL -->
</project>

Closing

After adjusting your pom.xml, don't forget to run the mvn clean compile command or perform a Reload Project in your IDE. Your generated-sources folder should now be populated with the metamodel classes you need.

Tidak ada komentar:

Posting Komentar