java – QueryDSL在构建谓词查询时添加交叉连接

我想从modelA中的modelA基础上获取数据.但对于我将在modelB中记录的每条记录,并非强制性的.因此,当我使用下面的代码来获取数据时,它返回0条记录.

BooleanExpression searchCriteria = searchCriteria
          .and(
                   qModelA.modelb.user.id.eq(userId)
              )
          .and ( some other conditions as well);

modelA.findAll(searchCriteria, pageable);

当我调试我发现QueryDsl放交叉连接.任何人都可以告诉我如何解决这个问题,是否有任何方式querydsl添加左连接而不是交叉连接?下面是我的两个型号.

@Entity
public class ModelA implements Serializable {

    private Long id;
    private String label;
    private ModelB modelB;

    @Id
    @SequenceGenerator(name = "modelASeq", sequenceName = "modela_id_seq", allocationSize = 1)
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "modelASeq")
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Column(length = 150, nullable = false)
    public String getLabel() {
        return label;
    }

    public void setLabel(String label) {
        this.label = label;
    }

    @OneToOne(mappedBy = "modelA", cascade = CascadeType.REMOVE)
    public ModelB getModelB() {
        return modelB;
    }

    public void setModelB(ModelB modelB) {
        this.modelB = modelB;
    }

}

@Entity
public class ModelB implements Serializable {

    private Long id;
    private User user;
    private ModelA modelA;

    @Id
    @SequenceGenerator(name = "modelBSeq", sequenceName = "modelb_id_seq", allocationSize = 1)
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "modelBSeq")
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @NotNull
    @ManyToOne
    @JoinColumn(name = "user_id", nullable = false )
    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    @NotNull
    @OneToOne
    @JoinColumn(name = "modela_id", nullable = false)
    public ModelA getModelA() {
        return modelA;
    }

    public void setModelA(ModelA modelA) {
        this.modelA = modelA;
    }

}
最佳答案
如果您需要左连接,则需要使用显式连接:

query.from(modelA)
     .leftJoin(modelA.modelB, modelB)
     ...

转载注明原文:java – QueryDSL在构建谓词查询时添加交叉连接 - 代码日志