问题描述
我在Java项目中将QueryDSL与Spring Data JPA一起使用,并且使用QueryDSL maven插件生成了文件,以使用由其生成的QueryDSL Model类.当我将其用于一级嵌套对象时,此方法效果很好,但是,如果尝试访问二级访问对象,它将给出NullPointerException,保存二级模型对象未初始化.
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.
不胜感激.
我在第三行qmachine中收到NullPointerException.供应商为空.
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);
我的Mapping类如下:交易
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;
}
并且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;
}
和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;
}
我故意省略了吸气剂和吸气剂.
I have ommitted the getters and setters intentionally.
推荐答案
默认情况下,仅初始化第一级.有关初始化选项,请参见此文档部分: http://www.querydsl.com/static/querydsl/3.6.0/reference/html/ch03s03.html#d0e2192
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
由于无限循环的可能性,最终字段无法进行完全深度初始化,但是Querydsl还提供了属性访问器方法的选项.
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生成的类无法访问第二级元素以进行查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!