QueryDSL 生成的类无法访问第二级元素进行查询
我在 Java 项目中使用 QueryDSL 和 Spring Data JPA,并使用 QueryDSL maven 插件生成文件来使用它生成的 QueryDSL 模型类。当我将它用于一级嵌套对象时,这非常有用,但是如果我尝试访问第二级访问对象,它会给出 NullPointerException ,保存第二级模型对象未初始化。
希望得到一些帮助。
我在第三行 qmachine.vendor 为 null 时收到 NullPointerException。
QTransaction qtransaction = QTransaction.transaction;
QMachine qmachine = qtransaction.machine;
BooleanExpression vendorexp = qmachine.vendor.vendor.eq(machineType);
我的映射课程如下: 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;
}
和 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;
}
和 Vendor 类是
@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;
}
我故意省略了 getter 和 setter。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
默认情况下,仅初始化第一级。有关初始化选项,请参阅此文档部分: http:// /www.querydsl.com/static/querydsl/3.6.0/reference/html/ch03s03.html#d0e2192
完整的深度初始化是由于可能存在无限循环,最终字段不可能,但 Querydsl 还提供属性访问器方法的选项。
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
您需要使用
@QueryInit("vendor.vendor")
在您的Transaction.machine
属性上https://github.com/querydsl /querydsl/issues/2129
http://www.querydsl.com/static/querydsl/2.2.4/reference/html/ch03s02.html
you need to use
@QueryInit("vendor.vendor")
on yourTransaction.machine
attributehttps://github.com/querydsl/querydsl/issues/2129