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

发布于 2024-11-15 16:23:27 字数 1333 浏览 3 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

黒涩兲箜 2024-11-22 16:23:27

默认情况下,仅初始化第一级。有关初始化选项,请参阅此文档部分: 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.

爱已欠费 2024-11-22 16:23:27

http://www.querydsl.com/static/ querydsl/2.2.4/reference/html/ch03s02.html

您需要使用 @QueryInit("vendor.vendor")在您的 Transaction.machine 属性上

@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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文