简体   繁体   English

Querydsl:字段未在子类上正确初始化

[英]Querydsl: a field is not properly initialised on the child class

Good day, 美好的一天,

We are using qdsl to access our inherited objects. 我们正在使用qdsl来访问继承的对象。 We have a parent class ( Parent ) which has a complicated object ( BasicProduct ) as a field. 我们有一个父类( Parent ),它具有一个复杂的对象( BasicProduct )作为字段。 Then we have several children (one of them is Child ). 然后,我们有几个孩子(其中一个是Child )。 In the query we are trying to access the BasicProduct from the QChild autogenerated code. 在查询中,我们尝试从QChild自动生成的代码访问BasicProduct From what we have seen - the field to access the BasicProduct exists on both the QParent and QChild but only the one on the QParent is correctly initialised. 从我们所看到的QParentQChild上都存在用于访问BasicProduct的字段,但是仅正确初始化了QParent上的QParent

The structure of the objects is: 对象的结构为:

Parent object - 父对象-

@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(
name="TYPE",
discriminatorType= DiscriminatorType.STRING)
public abstract class Parent

    //bunch of fields

    @OneToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "BASIC_PRODUCT_ID")
    protected BasicProduct basicProduct;
}

Child object - 子对象-

@DiscriminatorValue(value = "Child")
public class Child extends Parent {
    //bunch of fields
}

The access query: 访问查询:

HibernateQuery query = new HibernateQuery(session);

query = query.distinct().from(thing)
.leftJoin(thing.childSet, child)
.leftJoin(child.basicProduct(), basicProduct)
.leftJoin(child.basicProduct().fieldA, fieldA)
.leftJoin(basicProduct.fieldB, fieldB);

The access query above fails to build. 上面的访问查询无法建立。 What are we doing wrong? 我们做错了什么?

Looks like you have mixed types of entity accessor generation. 看起来您有多种类型的实体访问器生成。 Parent seems to have entityAccessors and Child does not. 父级似乎有entityAccessors,而子级没有。 This could lead to issues when the fields in QChild do not get initialized. 当QChild中的字段未初始化时,这可能导致问题。

Try to set entityAccessors on QChild too. 也尝试在QChild上设置entityAccessors。

If you look at your generated QChild , it should contain the fields inherited from its parent (with a ///inherited comment above the field). 如果查看生成的QChild ,它应该包含从其父级继承的字段(在该字段上方带有///inherited注释)。 All inherited fields are public final , so you can use them. 所有继承的字段都是public final ,因此您可以使用它们。

That said, basicProduct is in QChild , and you can access it - remove brackets from your code 也就是说, basicProductQChild ,您可以访问它-从代码中删除方括号

instead of 代替

.leftJoin(child.basicProduct(), basicProduct)

you should have 你应该有

.leftJoin(child.basicProduct, basicProduct)

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

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