Search code examples
spring-data-jpaquerydsl

Null pointer when using QueryDSL subtypes


I am trying to create a subtype query along the following lines, but tyre is coming back as null even if I set @QueryInit("tyre") on the wheel property of car.

QWheel wheel = QCar.car.wheel;
QTyre tyre = wheel.as(QRoadWheel.class).tyre;    
BooleanExpression tyreFittedOverYearAgo 
    = tyre.fitted.lt(today.minusYears(1));
Iterable<Car> carsWithOldTyres = repo.findAll(tyreFittedOverYearAgo);

How do I get QueryDSL to initialise tyre when it is accessed using as()?


Solution

  • By default Querydsl initializes only direct reference properties. In cases where longer initialization paths are required, these have to be annotated in the domain types via com.mysema.query.annotations.QueryInit usage. QueryInit is used on properties where deep initializations are needed.

    @Entity      
    class Event {
        @QueryInit("customer")
        Account account;
    }      
    
    @Entity
    class Account{
        Customer customer;    
    }
    
    @Entity
    class Customer{
        String name;
        String address;
    } 
    

    This will intialize customer.name ,customer.address