0

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"
4javier
  • 481
  • 2
  • 7
  • 22
  • 1
    What's the package that you expect? I think the generated classes should be in the same package as the corresponding entities, so they don't need to import the entities. Also, what version of Gradle are you using? You can use `jpaModelgenSourcesDir` to specify the generated files directory. – Mustafa Feb 04 '20 at 23:51
  • Eclipse complains that the declared package is differenti than the expected one. – 4javier Feb 05 '20 at 00:50
  • 1
    I think it does because it doesn't know that generated/sources/annotationProcessor/java/main is a source root folder. You need specify this in your gradle project and maybe reimport your project to eclipse. I gradle you use `sourceSets' to do that. – Mustafa Feb 05 '20 at 01:09
  • I tried to set the srcdir like this `sourceSets { main.java.srcDir 'build/generated/sources/annotationProcessor/java/main/petmenu/entities/' }` But nothing changes. I updated my original question with the error thrown by Eclipse on the package. Take a look, please. – 4javier Feb 05 '20 at 09:29

1 Answers1

1

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"

4javier
  • 481
  • 2
  • 7
  • 22