简体   繁体   中英

QueryDSL Generated classes not able to access second level elements for querying

I am using QueryDSL with Spring Data JPA in my Java Project and have Generated files using QueryDSL maven plugin to use the QueryDSL Model classes generated by it. This works great when i use it for one level nested objects, however if i try to access the 2nd level access objects it gives a NullPointerException saving the 2nd level model object is not initialized.

Would appreciate some help.

I am getting a NullPointerException in 3rd line qmachine.vendor is null.

QTransaction qtransaction = QTransaction.transaction;
QMachine qmachine = qtransaction.machine;
BooleanExpression vendorexp = qmachine.vendor.vendor.eq(machineType);

My Mapping classes are below: Transaction

@Entity
@Table(name = "dsdsd")
public class Transaction extends AbstractPersistable<Long> {

    private static final long serialVersionUID = 1L;

    @ManyToOne
    @JoinColumn(name = "machine_id")
    private Machine machine;

}

And the Machine class is :

@Entity
@Table(name="machine")
public class Machine extends AbstractPersistable<Long> {

    private static final long serialVersionUID = 1L;

    @ManyToOne
    @JoinColumn(name="vendor_id")
    private Vendor vendor;
}

and the Vendor class is

@Entity
@Table(name="vendors")
public class Vendor extends AbstractPersistable<Long> {

    private static final long serialVersionUID = 1L;

    @Column(name="vendor")
    @Enumerated(EnumType.STRING)
    private VendorType vendor;

}

I have ommitted the getters and setters intentionally.

By default only the first level is initialized. See this documentation section for initialization options : http://www.querydsl.com/static/querydsl/3.6.0/reference/html/ch03s03.html#d0e2192

Full deep initialization is not possible with final fields, because of the possibility of infinite loops, but Querydsl provides also the option of property accessor methods.

http://www.querydsl.com/static/querydsl/2.2.4/reference/html/ch03s02.html

you need to use @QueryInit("vendor.vendor") on your Transaction.machine attribute

@Entity
@Table(name = "dsdsd")
public class Transaction extends AbstractPersistable<Long> {

    private static final long serialVersionUID = 1L;

    @ManyToOne
    @JoinColumn(name = "machine_id")
    @QueryInit("vendor.vendor")
    private Machine machine;

}

https://github.com/querydsl/querydsl/issues/2129

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM