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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With