Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 881
Abhishek Avatar asked Jun 17 '11 11:06

Abhishek


1 Answers

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

like image 52
Julien Avatar answered Oct 21 '22 10:10

Julien