hibernategradlespring-data-jpajpamodelgen

JPA correct location and packaging of metamodels


I'm trying to make my project generates JPA metamodels for my entities. I'm using gradle on my project, so I added this line to my build.gradle dependencies object

annotationProcessor('org.hibernate:hibernate-jpamodelgen')

It actually works and metamodels are generated inside build dir of my project (not src!) under

generated.sources.annotationProcessor.java.main.petmenu.entities

The problem is that they declare the same package of their source entities instead of their expected one, and they don't even import their source entities classes, like if they reside into the same package.

package petmenu.entities;

import javax.annotation.Generated;
import javax.persistence.metamodel.SetAttribute;
import javax.persistence.metamodel.SingularAttribute;
import javax.persistence.metamodel.StaticMetamodel;

@Generated(value = "org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor")
@StaticMetamodel(Costituente.class)
public abstract class Costituente_ {
...
...
...
}

Obviously I could easily fix the package declaration and the missing imports, but it sounds strange to me that these tasks are not accomplished automatically. Am I doing something wrong?

EDIT: This is the error Eclipse throws on package declaration

The declared package "petmenu.entities" does not match the expected package "generated.sources.annotationProcessor.java.main.petmenu.entities"

Solution

  • Ok. I found the solution even if it's not toally clear to me. I got to set a custom directory for the generated metamodels. I achieved that adding these lines to build.gradle

    ext{
        generatedMetamodels = "${buildDir}/generated/"
    }
    sourceSets{
        main.java.srcDir generatedMetamodels
    }
    
    compileJava{
        options.annotationProcessorGeneratedSourcesDirectory = file(generatedMetamodels)
    }
    

    Sincerely, I still don't understand why this isn't processor's default behaviour. Anyway, with this customization Eclipse will not complain about mismatch between project structure and package declarations anymore.

    Remember to add generated directory to IDE source classpath. For Eclipse 4.14 (actually Eclipse Equinox from SpringToolSuite 4.5.0):

    Project→Properties→Java Build Path→Add folder→"$(YourProjectName)/build/generated"