The class generated by Java – querydsl cannot access the second level element for query
I use querydsl with spring data JPA in my java project, and use the file generated by querydsl Maven plug-in to use the querydsl model class generated by it This is useful when I use it for a level of nested objects, but if I try to access a second level access object, it will give a NullPointerException to keep the second level model object uninitialized
I'll thank you for some help
I'm on the third line qmachine Received a NullPointerException when vendor is null
QTransaction qtransaction = QTransaction.transaction; QMachine qmachine = qtransaction.machine; BooleanExpression vendorexp = qmachine.vendor.vendor.eq(machineType);
My mapping course is as follows: Trading
@Entity @Table(name = "dsdsd") public class Transaction extends AbstractPersistable<Long> { private static final long serialVersionUID = 1L; @ManyToOne @JoinColumn(name = "machine_id") private Machine machine; }
The machine types are:
@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 vendor classes are
@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 deliberately mentioned smokers and drug addicts
Solution
By default, only the first level is initialized For initialization options, see the section of this document: http://www.querydsl.com/static/querydsl/3.6.0/reference/html/ch03s03.html#d0e2192
Due to the possibility of infinite loop, the final field cannot be fully initialized, but querydsl also provides the option of property accessor method