1

We have a git multimodules projects MAVEN on IntelliJ. We use hibernate-jpamodelgen for criteria builder API.

We have web project using maven dependency entities library which are generating annotated class in entities => target folder.

When launching Install and test from mvn terminal it is working like a charm, but the issue is if we want to debug a test we have to launch with

-Dmaven.surefire.debug

option and with a remote application. It takes a time and not efficient. The problem is when we try to launch with right maven configuration (in IntelliJ) or directly in the class clicking the "play button" close to the method name it does not work, because it is creating in the web project all annotated classes but with empty body

for example what is in the dependency lib

@Generated(value = "org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor", date = "2022-12-30T10:11:22.168+0400")
@SuppressWarnings({ "deprecation", "rawtypes" })
@StaticMetamodel(FixedAsset.class)
public abstract class FixedAsset_ extends com.seanergie.persistence.ObjectWithUnidAndVersion_ {

    public static volatile SingularAttribute<FixedAsset, LocalDate> purchaseDate;
    public static volatile SingularAttribute<FixedAsset, String> serialNumber;
    public static volatile SingularAttribute<FixedAsset, MutableMoney> cost;
    public static volatile SingularAttribute<FixedAsset, String> notes;
    public static volatile SingularAttribute<FixedAsset, FixedAssetFilesStore> filesStore;

and what is created in other dependency target annotated class folder if i try to launch from intellij

@Generated(value = "org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor", date = "2022-12-30T10:12:05.141+0400")
@SuppressWarnings({ "deprecation", "rawtypes" })
@StaticMetamodel(FixedAsset.class)
public abstract class FixedAsset_ extends com.seanergie.persistence.ObjectWithUnidAndVersion_ {



}

As you can see class is empty so compilation not work enter image description here

We should be able to launch directly without creating a test configuration and modifying it (we have more than 100 test classes, so we can't create a conf for each one).

We see also that it try to compile all projects on each test enter image description here

we tried to add in pom.xml

<dependency><!-- For launch tests directly from IntelliJ play button-->
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-jpamodelgen</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>com.sun.activation</groupId>
                    <artifactId>jakarta.activation</artifactId>
                </exclusion>
            </exclusions>
            <scope>provided</scope>
        </dependency>

We also tried to deactivate or activate annotation processor in IntelliJ settings but nothing works

here in web project the hibernate mapping

<properties>
        <mainClass>com.intranet.Main</mainClass>
        <datasource.uri>jdbc:postgresql://localhost:5432/intranet?charSet=utf-8&amp;amp;ApplicationName=${DATASOURCE_APPLICATION_NAME}</datasource.uri>
        <datasource.pool_size.min>15</datasource.pool_size.min>
        <datasource.pool_size.max>30</datasource.pool_size.max>
        <hibernate.mapping.files>
            <![CDATA[
            <mapping-file>com/intranet-base.entities.xml</mapping-file>
            <mapping-file>com/intranet-webapp.entities.xml</mapping-file>
            <mapping-file>com/intranet-intranet.entities.xml</mapping-file>
            <mapping-file>com/intranet-webapp.entities.xml</mapping-file>
            <mapping-file>com/intranet2-intranet.entities.xml</mapping-file>
            ]]>
        </hibernate.mapping.files>
        <databaseInitializationHook.class>com.intranet.persistence.initialization.DatabaseInitializationHook</databaseInitializationHook.class>
        <test.databaseInitializationHook.class>com.intranet.persistence.initialization.DatabaseInitializationHook</test.databaseInitializationHook.class>
    </properties>

So finally when we launch mvn clean install even from IntelliJ (not only terminal) it create all annotated classes correctly in each library where we defined entities, but when we launch test it create another time same classes, but with empty body and in projects where entities are not defined (but use others as dependencies !)

What is the good way to make it work?

cyril
  • 872
  • 6
  • 29

1 Answers1

1

I do believe I have solved your puzzle.

The appearance of "empty" metamodel classes could be caused by one of the following:

  • some of plenty IDEA plugins influences on compile process and, obviously, fails
  • jpamodelgen annotation processor may perform extra work

And the last reason seems to be the actual one:

persistenceXml:

Per default the processor looks in /META-INF for persistence.xml. Specifying this option a persitence.xml file from a different location can be specified (has to be on the classpath)

ormXml:

Allows to specify additional entity mapping files. The specified value for this option is a comma separated string of mapping file names. Even when this option is specified /META-INF/orm.xml is implicit.

fullyAnnotationConfigured:

If set to true the processor will ignore orm.xml and persistence.xml.

I have checked and indeed placing META-INF/orm.xml with defined "foreign" entities into resources folder causes jpamodelgen to generate empty metamodel classes - as for me it looks like a bug.

You have following options (actually, depends on your project structure):

  • remove jpamodelgen from module dependencies if module does not define entity classes, also make sure dependency on jpamodelgen is defined as non-transitive, i.e. <scope>provided</scope>
  • rename extra xml mapping files - if you are on spring it is possible to specify those mappings via spring.jpa.mapping-resources
  • configure annotation processor as described in JBoss guide

UPD. based on @cyril comment.

First of all, defining dependencies (not imposing versions via <dependencyManagement>) in parent pom is a terrible idea - you have no chance to exclude such dependency in child modules, so, your parent pom is definitely broken.

However, I do believe you can meet your objectives (i.e. running tests via "green play button") even with broken parent pom - just follow JBoss guide and configure maven-compiler-plugin like:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
        <compilerArgs>
            <arg>-AfullyAnnotationConfigured=true</arg>
        </compilerArgs>
    </configuration>
</plugin>

IntelliJ does recognise such configuration.

Andrey B. Panfilov
  • 4,324
  • 2
  • 12
  • 18
  • i think it is the issue, but i don't know if i will manage to fix it, jpamodelgen is added in parent pom for maven model generation, indeed i have the xml file to specify entities in entities modules, and annotation processor activaed, i tried to deactivate annotation processor and rely only on the maven generation, but when i not compile test files, it crash :/, i would use the classes already generated it would be more easier no ? imagine i build project with maven command line, then i launch test , it is more quick to just launch test and compile test classes ? – cyril Jan 01 '23 at 09:44
  • @cyril pls check upd. – Andrey B. Panfilov Jan 01 '23 at 11:07
  • When i said in parent pom i mean in dependencyManagement so in fact it is ok, i managed to make it work in deactivating annotation processor in settings for almost all projects, and you were right i think the issue is because in web project, in pom.xml we define all mapping files needed(and annotation processor try to create annotated classes wrongly in target), we also already have the maven compiler plugin i will update question – cyril Jan 02 '23 at 04:50
  • I vote because the point that mapping files configuration make annotation processor making weird stuffs is causing the issue and help me to manage to make it work. We can simply deactivate annotation processor and then build all projects before... or activated annotation only for projects needed, thank you – cyril Jan 02 '23 at 06:08