2
votes

I created a maven project in sts and I am trying to create a war file for the project. I also have querydsl configured for my classes. One class is giving me a error response when I run maven install. ERROR: Error creating bean with name 'productBrandController' defined in file [C:\Users\vince\Documents\workspace-spring-tool-suite-4-4.0.1.RELEASE\project\target\test-classes\com\project\project\StoreBased\ProductBrandController.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'productBrandRepository': Invocation of init method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.mongodb.repository.support.QuerydslMongoPredicateExecutor]: Constructor threw exception; nested exception is java.lang.IllegalArgumentException: Did not find a query class project.QProductBrand for domain class project.StoreBased.ProductBrand! I need help with my pom file (Not sure if javax.xml.bind might be the culprit) and I am also not sure if my output directory is correct (I already tried java instead of annotations as output). Much thanks for the help!

This is the class

import java.util.ArrayList;
import java.util.List;

import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.index.IndexDirection;
import org.springframework.data.mongodb.core.index.Indexed;
import org.springframework.data.mongodb.core.mapping.Document;


@Document(collection = "ProductBrand")
public class ProductBrand {

    @Id
    private String id;
    private ProductsBasic basic;
    @Indexed(direction = IndexDirection.ASCENDING)
    private String name;
    @Indexed(direction = IndexDirection.ASCENDING)
    private String units;
    @Indexed(direction = IndexDirection.ASCENDING)
    private double price;
    @Indexed(direction = IndexDirection.ASCENDING)
    private double price100g;
    private String manufacturer;
    private List<Manufacturer> manufacturerIndex;
    private boolean organic;
    private List<Organic> organicIndex;
    private String category;
    private List<Category> categoryIndex;
    private String subCategory;
    private List<SubCategory> subCategoryIndex;
    private boolean seasonal;
    private List<Seasonal> seasonalIndex;
    private boolean frozenGoods;
    private List<FrozenGoods> frozenGoodsIndex;
    private boolean refrigeratedGoods;
    private List<RefrigeratedGoods> refrigeratedGoodsIndex;
    private boolean available;
    private List<Available> availableIndex;
    @Indexed(direction = IndexDirection.ASCENDING)
    private String store;
    private List<Store> storeIndex;

    // public ProductBrand() {
    // super();
    // }

    public ProductBrand() {
        this.setAvailableIndex(new ArrayList<>());
        this.setCategoryIndex(new ArrayList<>());
        this.setSubCategoryIndex(new ArrayList<>());
        this.setFrozenGoodsIndex(new ArrayList<>());
        this.setManufacturerIndex(new ArrayList<>());
        this.setOrganicIndex(new ArrayList<>());
        this.setRefrigeratedGoodsIndex(new ArrayList<>());
        this.setSeasonalIndex(new ArrayList<>());
        this.setStoreIndex(new ArrayList<>());
    }

    public ProductBrand(String id, ProductsBasic basic, String name, String units, double price, double price100g,
        String manufacturer, List<Manufacturer> manufacturerIndex, boolean organic, List<Organic> organicIndex,
        String category, List<Category> categoryIndex, String subCategory, List<SubCategory> subCategoryIndex,
        boolean seasonal, List<Seasonal> seasonalIndex, boolean frozenGoods, List<FrozenGoods> frozenGoodsIndex,
        boolean refrigeratedGoods, List<RefrigeratedGoods> refrigeratedGoodsIndex, boolean available,
        List<Available> availableIndex, String store, List<Store> storeIndex) {
        super();
        this.id = id;
        this.basic = basic;
        this.name = name;
        this.units = units;
        this.price = price;
        this.price100g = price100g;
        this.manufacturer = manufacturer;
        this.setManufacturerIndex(manufacturerIndex);
        this.organic = organic;
        this.setOrganicIndex(organicIndex);
        this.category = category;
        this.setCategoryIndex(categoryIndex);
        this.subCategory = subCategory;
        this.setSubCategoryIndex(subCategoryIndex);
        this.seasonal = seasonal;
        this.setSeasonalIndex(seasonalIndex);
        this.frozenGoods = frozenGoods;
        this.setFrozenGoodsIndex(frozenGoodsIndex);
        this.refrigeratedGoods = refrigeratedGoods;
        this.setRefrigeratedGoodsIndex(refrigeratedGoodsIndex);
        this.available = available;
        this.setAvailableIndex(availableIndex);
        this.store = store;
        this.setStoreIndex(storeIndex);
    }

    public String getManufacturer() {
        return manufacturer;
    }

    public void setManufacturer(String manufacturer) {
        this.manufacturer = manufacturer;
    }

    public boolean isOrganic() {
        return organic;
    }

    public void setOrganic(boolean organic) {
        this.organic = organic;
    }

    public String getCategory() {
        return category;
    }

    public void setCategory(String category) {
        this.category = category;
    }

    public String getSubCategory() {
        return subCategory;
    }

    public void setSubCategory(String subCategory) {
        this.subCategory = subCategory;
    }

    public boolean isSeasonal() {
        return seasonal;
    }

    public void setSeasonal(boolean seasonal) {
        this.seasonal = seasonal;
    }

    public boolean isFrozenGoods() {
        return frozenGoods;
    }

    public void setFrozenGoods(boolean frozenGoods) {
        this.frozenGoods = frozenGoods;
    }

    public boolean isRefrigeratedGoods() {
        return refrigeratedGoods;
    }

    public void setRefrigeratedGoods(boolean refrigeratedGoods) {
        this.refrigeratedGoods = refrigeratedGoods;
    }

    public boolean isAvailable() {
        return available;
    }

    public void setAvailable(boolean available) {
        this.available = available;
    }

    public void addProduktBrand(ProductBrand productBrand) {
        ProductBrand.add(productBrand);
    }

    public static void add(ProductBrand productBrand) {
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public ProductsBasic getBasic() {
        return basic;
    }

    public void setBasic(ProductsBasic basic) {
        this.basic = basic;
    }

    public String getStore() {
        return store;
    }

    public void setStore(String store) {
        this.store = store;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getUnits() {
        return units;
    }

    public void setUnits(String units) {
        this.units = units;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public double getPrice100g() {
        return price100g;
    }

    public void setPrice100g(double price100g) {
        this.price100g = price100g;
    }

    public List<Manufacturer> getManufacturerIndex() {
        return manufacturerIndex;
    }

    public void setManufacturerIndex(List<Manufacturer> manufacturerIndex) {
        this.manufacturerIndex = manufacturerIndex;
    }

    public List<Organic> getOrganicIndex() {
        return organicIndex;
    }

    public void setOrganicIndex(List<Organic> organicIndex) {
        this.organicIndex = organicIndex;
    }

    public List<Category> getCategoryIndex() {
        return categoryIndex;
    }

    public void setCategoryIndex(List<Category> categoryIndex) {
        this.categoryIndex = categoryIndex;
    }

    public List<SubCategory> getSubCategoryIndex() {
        return subCategoryIndex;
    }

    public void setSubCategoryIndex(List<SubCategory> subCategoryIndex) {
        this.subCategoryIndex = subCategoryIndex;
    }

    public List<Seasonal> getSeasonalIndex() {
        return seasonalIndex;
    }

    public void setSeasonalIndex(List<Seasonal> seasonalIndex) {
        this.seasonalIndex = seasonalIndex;
    }

    public List<FrozenGoods> getFrozenGoodsIndex() {
        return frozenGoodsIndex;
    }

    public void setFrozenGoodsIndex(List<FrozenGoods> frozenGoodsIndex) {
        this.frozenGoodsIndex = frozenGoodsIndex;
    }

    public List<RefrigeratedGoods> getRefrigeratedGoodsIndex() {
        return refrigeratedGoodsIndex;
    }

    public void setRefrigeratedGoodsIndex(List<RefrigeratedGoods> refrigeratedGoodsIndex) {
        this.refrigeratedGoodsIndex = refrigeratedGoodsIndex;
    }

    public List<Available> getAvailableIndex() {
        return availableIndex;
    }

    public void setAvailableIndex(List<Available> availableIndex) {
        this.availableIndex = availableIndex;
    }

    public List<Store> getStoreIndex() {
        return storeIndex;
    }

    public void setStoreIndex(List<Store> storeIndex) {
        this.storeIndex = storeIndex;
    }

}

This is the controller of the class

import java.util.List;
import java.util.Optional;

import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import project.QProductBrand;
import com.querydsl.core.types.dsl.BooleanExpression;


@RestController
@RequestMapping("/productbrand")
public class ProductBrandController {
    private ProductBrandRepository productBrandRepository;

    public ProductBrandController(ProductBrandRepository productBrandRepository) {
        this.productBrandRepository = productBrandRepository;
    }

    @GetMapping("/all")
    public List<ProductBrand> getAll() {
        List<ProductBrand> product = this.productBrandRepository.findAll();

        return product;
    }

    @GetMapping("/productbrand/{name}")
    public List<ProductBrand> getByName(@PathVariable("name") String name) {
        List<ProductBrand> product = this.productBrandRepository.findByName(name);

        return product;
    }

    @PutMapping
    public void insert(@RequestBody ProductBrand productBrand) {
        this.productBrandRepository.insert(productBrand);
    }

    @PostMapping
    public void update(@RequestBody ProductBrand productBrand) {
        this.productBrandRepository.save(productBrand);
    }

    @DeleteMapping("/{id}")
    public void delete(@PathVariable("id") ProductBrand id) {
        this.productBrandRepository.delete(id);
    }

    @GetMapping("/{id}")
    public Optional<ProductBrand> getById(@PathVariable("id") String id) {
        Optional<ProductBrand> brand = this.productBrandRepository.findById(id);
        return brand;

    }

    @GetMapping("/productBrand/{organic}")
    public List<ProductBrand> getByOrganic(@PathVariable("organic") boolean organic) {
        List<ProductBrand> brand = this.productBrandRepository.findByOrganic(organic);

        return brand;
    }

    @GetMapping("/productBrand/{manufacturer}")
    public List<ProductBrand> getByManufacturer(@PathVariable("manufacturer") String manufacturer) {
        List<ProductBrand> brand = this.productBrandRepository.findByManufacturer(manufacturer);

        return brand;
    }

    @GetMapping("/productBrand/{category}")
    public List<ProductBrand> getByCategory(@PathVariable("category") String category) {
        List<ProductBrand> brand = this.productBrandRepository.findByCategory(category);

        return brand;
    }

    @GetMapping("/productBrand/{seasonal}")
    public List<ProductBrand> getBySeasonal(@PathVariable("seasonal") boolean seasonal) {
        List<ProductBrand> brand = this.productBrandRepository.findBySeasonal(seasonal);

        return brand;
    }

    @GetMapping("/productBrand/{frozenGoods}")
    public List<ProductBrand> getByFrozenGoods(@PathVariable("frozenGoods") boolean frozenGoods) {
        List<ProductBrand> brand = this.productBrandRepository.findByFrozenGoods(frozenGoods);

        return brand;
    }

    @GetMapping("/productBrand/{refrigeratedGoods}")
    public List<ProductBrand> getByRefrigeratedGoods(@PathVariable("refrigeratedGoods") boolean refrigeratedGoods) {
        List<ProductBrand> brand = this.productBrandRepository.findByRefrigeratedGoods(refrigeratedGoods);

        return brand;
    }

    @GetMapping("/productBrand/{available}")
    public List<ProductBrand> getByAvailable(@PathVariable("available") boolean available) {
        List<ProductBrand> brand = this.productBrandRepository.findByAvailable(available);

        return brand;
    }


    @GetMapping("/recommended")
    public List<ProductBrand> getRecommended() {
        final boolean isOrganic = true;
        final boolean isSeasonal = true;

        QProductBrand qBrand = new QProductBrand("ProductBrand");

        BooleanExpression filterByOrganic = (qBrand.organicIndex).any().organic.gt(isOrganic);

        BooleanExpression filterBySeasonal = (qBrand.seasonalIndex).any().seasonal.gt(isSeasonal);

        List<ProductBrand> brand = (List<ProductBrand>) this.productBrandRepository
                .findAll(filterByOrganic.and(filterBySeasonal));

        return brand;
    }

}

This is my repository

import java.util.List;

import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.querydsl.QuerydslPredicateExecutor;
import org.springframework.stereotype.Repository;

@Repository
public interface ProductBrandRepository extends MongoRepository<ProductBrand, String>, QuerydslPredicateExecutor<ProductBrand> {

    List<ProductBrand> findByName(String name);

    List<ProductBrand> findByOrganic(boolean organic);

    List<ProductBrand> findBySeasonal(boolean seasonal);

    List<ProductBrand> findByFrozenGoods(boolean frozenGoods);

    List<ProductBrand> findByRefrigeratedGoods(boolean refrigeratedGoods);

    List<ProductBrand> findByAvailable(boolean available);

    List<ProductBrand> findByCategory(String category);

    List<ProductBrand> findByManufacturer(String manufacturer);

}

And this is my pom file

<?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.BETAapp</groupId>
    <artifactId>project</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>

    <name>project</name>
    <description>demo</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.6.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
        </dependency>
        <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-mongodb</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-rest</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-groovy-templates</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mustache</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web-services</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
    </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>
            <version>2.4.0-b180725.0427</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.mongodb.morphia/morphia -->
        <dependency>
            <groupId>org.mongodb.morphia</groupId>
            <artifactId>morphia</artifactId>
            <version>0.107</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/javax.persistence/javax.persistence-api -->
        <dependency>
            <groupId>javax.persistence</groupId>
            <artifactId>javax.persistence-api</artifactId>
            <version>2.2</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.querydsl/querydsl-apt -->
        <dependency>
            <groupId>com.querydsl</groupId>
            <artifactId>querydsl-mongodb</artifactId>
            <version>4.2.1</version>
            <exclusions>
                <exclusion>
                    <groupId>org.mongodb</groupId>
                    <artifactId>mongo-java-driver</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>com.mysema.maven</groupId>
                <artifactId>apt-maven-plugin</artifactId>
                <version>1.1.3</version>
                <dependencies>
                    <dependency>
                        <groupId>com.querydsl</groupId>
                        <artifactId>querydsl-apt</artifactId>
                        <version>4.2.1</version>
                    </dependency>
                </dependencies>
                <executions>
                    <execution>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>process</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>target/generated-sources/annotations</outputDirectory>
                            <processor>org.springframework.data.mongodb.repository.support.MongoAnnotationProcessor</processor>
                            <logOnlyOnError>true</logOnlyOnError>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>


</project>

Test set: project.StoreBased.ApplicationTests

Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 11.036 s <<< FAILURE! - in project.StoreBased.ApplicationTests contextLoads(project.StoreBased.ApplicationTests) Time elapsed: 0.002 s <<< ERROR! java.lang.IllegalStateException: Failed to load ApplicationContext Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'productBrandController' defined in file [C:\Users\vince\Documents\workspace-spring-tool-suite-4-4.0.1.RELEASE\project(18.11.2018)\target\test-classes\com\project\project\StoreBased\ProductBrandController.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'productBrandRepository': Invocation of init method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.mongodb.repository.support.QuerydslMongoPredicateExecutor]: Constructor threw exception; nested exception is java.lang.IllegalArgumentException: Did not find a query class project.StoreBased.QProductBrand for domain class project.StoreBased.ProductBrand! Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'productBrandRepository': Invocation of init method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.mongodb.repository.support.QuerydslMongoPredicateExecutor]: Constructor threw exception; nested exception is java.lang.IllegalArgumentException: Did not find a query class project.StoreBased.QProductBrand for domain class project.project.StoreBased.ProductBrand! Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.mongodb.repository.support.QuerydslMongoPredicateExecutor]: Constructor threw exception; nested exception is java.lang.IllegalArgumentException: Did not find a query class project.StoreBased.QProductBrand for domain class project.StoreBased.ProductBrand! Caused by: java.lang.IllegalArgumentException: Did not find a query class project.StoreBased.QProductBrand for domain class project.StoreBased.ProductBrand! Caused by: java.lang.ClassNotFoundException: project.StoreBased.QProductBrand

1

1 Answers

0
votes

Reason for this issue:

Your Query Classes are generated in different package (i.e. inside /annotations) and 
you are trying to find it from different package.

Solution:

Change the outputDirectory path (i.e. taget location for generating 'Q' prefixed Classes)

<outputDirectory>target/generated-sources/annotations</outputDirectory>

to

<outputDirectory>target/generated-sources/apt</outputDirectory>

Delete your target folder and do

mvn clean install