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:
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: