简体   繁体   English

QueryDSL 生成的类无法访问二级元素进行查询

[英]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.我在我的 Java 项目中使用带有 Spring Data JPA 的 QueryDSL,并使用 QueryDSL maven 插件生成文件来使用它生成的 QueryDSL 模型类。 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.当我将它用于一级嵌套对象时,这很有效,但是如果我尝试访问二级访问对象,它会给出 NullPointerException 保存二级模型对象未初始化。

Would appreciate some help.希望得到一些帮助。

I am getting a NullPointerException in 3rd line qmachine.vendor is null.我在第 3 行 qmachine.vendor 中收到 NullPointerException 为空。

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 :而 Machine 类是:

@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.我有意省略了 getter 和 setter。

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有关初始化选项,请参阅此文档部分: 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.由于存在无限循环的可能性,最终字段无法进行完全深度初始化,但 Querydsl 还提供了属性访问器方法的选项。

http://www.querydsl.com/static/querydsl/2.2.4/reference/html/ch03s02.html 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您需要在Transaction.machine属性上使用@QueryInit("vendor.vendor")

@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 https://github.com/querydsl/querydsl/issues/2129

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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