4

Eclipse does not recognize the Metamodel and I always get the error: "xy_ can not be resolved to a variable." I tried everything and spent a lot of time seraching for alternatives but somehow it just doesn`t work.

All in all I did following:

  • add dependency in pom.xml
  • set annotation processing (Project>Properties>Java Compiler>Annotation Processing)
  • set factory path (>Annotation Processing>Factory Path)
  • add generated classes folder to classpath like described here

Also I do not see the generated classes in the defined folders. Besides that I annotated all Entities and the relationships correctly.

What have I done wrong?

Java Class where metamodel is used and error occurs:

    public class ProductSpecs {

      public static Specification<Product> getProductsByFilter(String keyword, Integer categoryId,
       String postalCode) {
      return (root, cq, cb) -> {
        List<Predicate> predicates = new ArrayList<>();

        // join tables product, retailStore and address for correct filtering
        Join<Product, RetailStore> store = root.join(Product_.retailStore, JoinType.LEFT);
        Join<RetailStore, Address> address = store.join(RetailStore_.address, JoinType.LEFT);

        // create comparsion elements using CriteriaBuilder
        if (keyword != null) {
          Predicate keywordSearch = cb.like(cb.lower(address.get(Product_.name)),
              "%" + keyword.toLowerCase() + "%");
        predicates.add(keywordSearch);
        }
        if (categoryId != null) {
          Predicate categorySearch = cb.equal(address.get(Product_.categoryId), categoryId);
          predicates.add(categorySearch);
        }
        if (postalCode != null) {
          Predicate postalCodeSearch = cb.equal(address.get(Address_.postalCode), postalCode);
          predicates.add(postalCodeSearch);
        }

        // add selected predicates to where clause and build query
        return cq.where(predicates.toArray(new Predicate[0])).orderBy(cb.desc(root.get("name")))
            .getRestriction();
      };  
    }
  }

pom.xml:

<dependencies>
    <!-- thymeleaf -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    
    <!-- web mvc -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    
    <!-- hot swapping, disable cache for template, enable live reload -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
        <optional>true</optional>
    </dependency>
    
    <!-- test -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
        <exclusions>
            <exclusion>
                <groupId>org.junit.vintage</groupId>
                <artifactId>junit-vintage-engine</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

    <!-- authentication and authorization -->
    <dependency>
        <groupId>io.jsonwebtoken</groupId>
        <artifactId>jjwt</artifactId>
        <version>0.9.0</version>
    </dependency>
    
    <!-- database -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-jpamodelgen -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-jpamodelgen</artifactId>
        <version>5.3.7.Final</version>
        <scope>provided</scope>
    </dependency>
    
    <!-- lombok project -->
    <dependency>
  <groupId>org.projectlombok</groupId>
  <artifactId>lombok</artifactId>
  <version>1.18.12</version>
</dependency>
    
    <!-- security -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
    <dependency>
        <groupId>org.thymeleaf.extras</groupId>
        <artifactId>thymeleaf-extras-springsecurity5</artifactId>
    </dependency>
    
</dependencies>

<build>
    <plugins>
        <!-- Package as an executable jar/war -->
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
      <addResources>true</addResources>
    </configuration>
        </plugin>
    </plugins>
</build>

JavaCompiler-Annotation Processing Settings:

JavaCompiler-Annotation Processing Settings

FactoryPath Settings:

  • added external jar hibernate-jpa-modelgen-5.3.7.Final.jar
  • added external jar javax-persistence-2.0.3.jar

Java build path setting:

java build path setting

nin-prog
  • 91
  • 2
  • 8
  • Try to use `Factory Path > jaxb-api-2.3.1.jar` [jaxb-api-2.3.1.jar](https://mvnrepository.com/artifact/javax.xml.bind/jaxb-api/2.3.1) -- [reference source](https://hpratap.medium.com/configure-eclipse-ide-for-jpa-metamodel-generation-for-jhipster-project-efa97e2ab06e) – Nor.Z Apr 15 '22 at 20:57

1 Answers1

0

For some old libraries you will need to include JAXB on Factory Path.

For version 6.2.5 it does not need JAXB, but it works only when you include jakarta.xml.bind-api.

In my case I am using it from Wildfly 28 (.../wildfly-28.0.1.Final/modules/system/layers/base/jakarta/xml/bind/api/main/jakarta.xml.bind-api-4.0.0.jar)

You can check dependencies for you version on Maven Repository: https://mvnrepository.com/artifact/org.hibernate.orm/hibernate-jpamodelgen/6.2.5.Final

DHansen
  • 225
  • 3
  • 13